用于一些 UI 控件的单个 DoubleTapDetector
我正在尝试识别在 Android 中触发 MotionEvent 的 UI 控件。 一个 doubleTapDetector
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doubleTapDetector = new GestureDetector(this, new DoubleTapDetector());
}
声明了
private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
String uiControlName = obtainUiControlName(e);
// Do something depends on uiControlName
return true;
}
private String obtainUiControlName(MotionEvent e) {
int deviceId = e.getDeviceId();
switch (deviceId) {
case R.id.button1: return "Button1";
case R.id.button2: return "Button2";
}
return null;
}
}
我在两个按钮上
Button button1 = (Button) findViewById(R.id.button1);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
Button button2 = (Button) findViewById(R.id.button2);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
问题是 deviceId 始终等于 0 并且我无法识别哪个按钮触发双击事件。有没有办法在不为每个按钮实现两个不同的 doubleTapDetector 的情况下做到这一点?
I am trying to identify UI-control that fired MotionEvent in Android. I have one doubleTapDetector
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doubleTapDetector = new GestureDetector(this, new DoubleTapDetector());
}
declared as
private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
String uiControlName = obtainUiControlName(e);
// Do something depends on uiControlName
return true;
}
private String obtainUiControlName(MotionEvent e) {
int deviceId = e.getDeviceId();
switch (deviceId) {
case R.id.button1: return "Button1";
case R.id.button2: return "Button2";
}
return null;
}
}
placed on both buttons
Button button1 = (Button) findViewById(R.id.button1);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
Button button2 = (Button) findViewById(R.id.button2);
outcomeButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
doubleTapDetector.onTouchEvent(event);
return true;
}
});
The problem is that deviceId always equals 0 and I can't identify which button fires double click event. Is there a way to do that without implementing of two different doubleTapDetector's for each button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误。 getDeviceId 返回接收触摸的物理指针设备 ID。不是小部件 ID。
我觉得唯一的方法是为每个按钮创建唯一的 DoubleTapDetector,并在 DoubleTapDetector 类中存储某种按钮的 ID。因为 onDoubleTap 方法没有提供有关点击发生的 Widget 的足够信息。
像这样:
Mistake. getDeviceId returns physical pointer device id from where touch was received. Not Widget ID.
I feel that only way to do it is to create unique DoubleTapDetector for each your button, and also store some kind of button's ID in your DoubleTapDetector class. Because onDoubleTap method doesn't give enough info about Widget on which tap happened.
Like this: