【问题标题】:Android Disable Multitouch on ViewsAndroid 在视图上禁用多点触控
【发布时间】:2013-09-06 05:47:50
【问题描述】:

我面临多点触控问题。问题是我可以同时触摸屏幕上的两个按钮。

我知道这个问题在这个论坛上被问了好几次,唯一的解决方案是在你的父布局中声明 android:splitMotionEvents="false"。但是在宣布这一点之后,问题仍然存在。是硬件问题还是代码问题?此处的任何指针表示赞赏。

【问题讨论】:

  • 您尝试过使用 android:windowEnableSplitTouch 吗?原因将 android:windowEnableSplitTouch="false" 或 android:splitMotionEvents="false" 设置为按钮所在的视图组,有效。

标签: android button view multi-touch


【解决方案1】:

出现此问题是因为从 android 4.0 开始,每个 onClick 都在一个新线程中执行。

我是如何解决的:

//1.创建自己的点击监听器

    public abstract class AbstractCarOnClickListener {
        protected static volatile boolean processing = false;
        protected void executeBlock() {
            ActivityUtil.scheduleOnMainThread(new Runnable() {
                public void run() {
                    processing=false;
                }
            }, 400);
        }
    }

//2.创建监听器的子类

public abstract class AppButtonsOnClickListener extends AbstractCarOnClickListener implements View.OnClickListener {

    public void onClick(View v) {
        if(processing) return;
        try{
            processing=true;            
            onCarButtonClick(v);
        } finally {
            executeBlock();
        }
    }
    public abstract void onCarButtonClick(View v);
}

//3.为您的视图设置监听器

    public void onClick(View v) {
         clickListener.onClick(v);
    }

    public OnClickListener clickListener = new AppButtonsOnClickListener(){
        public void onCarButtonClick(View v) {
            hintContainer.setVisibility(View.GONE);
            if (v == cancelButton) {
                listener.onCancelButtonClicked();
            }
        }
    }

【讨论】:

    【解决方案2】:

    这对我有用。除了在每个包含按钮的 ViewGroup 上设置 android:splitMotionEvents="false" 我把它放在 MyAdapter.getView()...

        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View cell, MotionEvent event) {
                // Process touches only if: 1) We havent touched a cell, or 2) This event is on the touched cell
                if (mTouchedCell == null || cell.equals(mTouchedCell)) {
                    int action = event.getAction();
                    if (action == MotionEvent.ACTION_DOWN) {
                        cell.startAnimation(mShrink);
                        mTouchedCell = cell;
                    } else if (action == MotionEvent.ACTION_CANCEL) {
                        if (cell.equals(mTouchedCell)) {
                            cell.startAnimation(mGrow);
                        }
                        mTouchedCell = null;
                        return true;
                    } else if (action == MotionEvent.ACTION_UP) {
                        cell.startAnimation(mFadeOut);
                        mTouchedCell = null;
                    }
                    return false;
                }
                return true;
            }
        });
    

    ...当然这在适配器中...

    private static View mTouchedCell = null;
    

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多