【发布时间】: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