【问题标题】:onGenericMotionEvent on android buttonandroid按钮上的onGenericMotionEvent
【发布时间】:2015-10-18 17:43:20
【问题描述】:

我无法在按钮或任何视图上设置onGenericMotionEvent。表示我没有收到来自onGenericMotionEvent 方法的任何响应。

下面是我的代码:

<Button
                android:id="@+id/btnMouse"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

我的 Java 文件编码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_controls);
btn1 = (Button) findViewById(R.id.btnMouse);
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    int pointerCount = event.getPointerCount();

    switch (event.getAction() & event.ACTION_MASK) {
        case MotionEvent.ACTION_POINTER_DOWN:
            Log.e("Mouse: ", "Right Click");
            return true;
        case MotionEvent.ACTION_DOWN:
            Log.w("Mouse: ", "Left Click");
            return true;
        case MotionEvent.ACTION_HOVER_MOVE:
            if (pointerCount == 1) {
                Log.e("Mouse: ", "Move");
            } else if (pointerCount == 2) {
                Log.e("Mouse: ", "Scroll");
            }
            return true;
    }
    return false;
}

当我在按钮上使用setOnTouchListener 时,它工作得很好,但我无法在我的按钮上获得MotionEvent.ACTION_HOVER_MOVE,当我阅读文档时,它指定setOnTouchListener 无法处理MotionEvent.ACTION_HOVER_MOVE,这就是我想要的原因使用我的按钮上的onGenericMotionEvent 来获取MotionEvent.ACTION_HOVER_MOVE 操作。

或任何其他方式在按钮上实现MotionEvent.ACTION_HOVER_MOVE

但我不知道该怎么做,请帮忙

已编辑

通过 Android 按钮,我想模拟真正的鼠标,这意味着当用户拖动它而不是移动我的笔记本电脑鼠标时。

当我使用MotionEvent.ACTION_MOVE: 时,它可以工作并且它会移动我的笔记本电脑鼠标,但在我的鼠标移动之前它会更改笔记本电脑鼠标指针的位置,它点击按钮的位置,所以忽略Android 提供ACTION_HOVER_MOVE 它不是执行ACTION_DOWN 不像ACTION_MOVE

【问题讨论】:

  • 你需要 ACTION_HOVER_MOVE 做什么?
  • @pskink,我想要模拟真实鼠标,意味着当用户在按钮上移动手指时我想移动笔记本电脑鼠标。
  • @pskink,简而言之,我想处理来自我的 android 按钮的所有鼠标事件。
  • 然后覆盖Activity#dispatchTouchEvent
  • @pskink,它对我也不起作用,它不处理 ACTION_HOVER_MOVE 但处理其他所有事件

标签: android android-button multi-touch motionevent


【解决方案1】:

嘿,当我遇到您的问题时,您希望在触摸按钮时产生悬停效果。

您可以使用下面的代码,只需在此方法中传递您的 Button 实例,还可以自定义悬停效果的颜色:

public void buttonEffect(View button){
        button.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(Color.parseColor("#00BFFF"),PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    break;
                }

                case MotionEvent.ACTION_CANCEL:{
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    break;
                }
                }
                return false;
            }
        });
    }

希望对您有所帮助...!!!

【讨论】:

  • 我不想对我的按钮产生任何影响,我只想处理我的按钮上的MotionEvent.ACTION_HOVER_MOVE,请你帮我做吗?
猜你喜欢
  • 2017-08-17
  • 1970-01-01
  • 2013-03-02
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
相关资源
最近更新 更多