用于一些 UI 控件的单个 DoubleTapDetector

发布于 2024-12-07 05:09:15 字数 1639 浏览 1 评论 0原文

我正在尝试识别在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-12-14 05:09:15

错误。 getDeviceId 返回接收触摸的物理指针设备 ID。不是小部件 ID。

我觉得唯一的方法是为每个按钮创建唯一的 DoubleTapDetector,并在 DoubleTapDetector 类中存储某种按钮的 ID。因为 onDoubleTap 方法没有提供有关点击发生的 Widget 的足够信息。

像这样:

private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener { 
   int id;
   DoubleTapDetector(int id){
      this.id = id;
   }
...

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:

private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener { 
   int id;
   DoubleTapDetector(int id){
      this.id = id;
   }
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文