【问题标题】:Multiple ACTION_DOWN events do the same多个 ACTION_DOWN 事件做同样的事情
【发布时间】:2013-04-10 15:52:39
【问题描述】:

我想让 3 个按钮一次可触摸,这样您就可以触摸一个按钮而无需从另一个按钮上移开手指。到目前为止,我按以下顺序使用 ACTION_DOWN、ACTION_POINTER_1_DOWN 和 ACTION_POINTER_2_DOWN:

      public boolean onTouch(final View v, final MotionEvent event) {


            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                switch (v.getId()) {
                case R.id.button1:
                {   playSound(SOUND1,  1.0f) ;};
                    break;
                case R.id.button2:
                {   playSound(SOUND2,  1.0f) ;};
                    break;
                case R.id.button3:
                {   playSound(SOUND3,  1.0f) ;};
                    break;}

            } else if (event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                switch (v.getId()) {
                case R.id.button1:
                {   playSound(SOUND1,  1.0f) ;};
                    break;
                case R.id.button2:
                {   playSound(SOUND2,  1.0f) ;};
                    break;
                case R.id.button3:
                {   playSound(SOUND3,  1.0f) ;};

                    break;}

            } else if (event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                switch (v.getId()) {
                case R.id.button1:
                {   playSound(SOUND1,  1.0f) ;};

                    break;
                case R.id.button2:
                {   playSound(SOUND2,  1.0f) ;};

                    break;
                case R.id.button3:
                {   playSound(SOUND3,  1.0f) ;};
                    break;}

如果我在手机上编译和运行这个项目,没有错误或警告。

但是当我进行第二次触摸时,会播放与第一次相同的声音。 示例:手指 2 按下按钮 2(声音 2 播放)。将手指 2 放在按钮 2 上时,我用手指 3 触摸按钮 3。我预计,声音 3 正在播放,但声音 2 再次播放。

我已经尝试过“return true”,但这并没有改变任何东西。

一些人也有这个问题,但没有人真正在这些线程中找到解决方案。

亚历克斯

更新:我刚刚发现点击的位置并不重要。即使我点击空白区域,另一根手指所在的按钮也会再次做出反应

【问题讨论】:

    标签: android button multi-touch


    【解决方案1】:

    我注意到您使用的代码已被弃用。我相信以下代码(从 Android 培训文档中获取,链接如下)应该有助于解决您的问题。

    http://developer.android.com/training/gestures/multi.html

    private int mActivePointerId;
    
    public boolean onTouchEvent(MotionEvent event) 
    {
        ....
        // Get the pointer ID
        mActivePointerId = event.getPointerId(0);
    
        // ... Many touch events later...
    
        // Use the pointer ID to find the index of the active pointer 
        // and fetch its position
        int pointerIndex = event.findPointerIndex(mActivePointerId);
        // Get the pointer's current position
        float x = event.getX(pointerIndex);
        float y = event.getY(pointerIndex);
    }
    

    然后,您可以使用 x、y 坐标来确定他们正在按下哪个按钮,并激活您的声音。

    【讨论】:

    • 非常感谢,我不知道。你能告诉我,我怎样才能让应用程序忽略 onTouch,所以 OnTouchEvent 被调用?会很棒
    • 您必须让自定义类扩展 View 并覆盖 onTouchEvent...请参阅grepcode.com/file/repository.grepcode.com/java/ext/…的源代码
    • 非常感谢先生,我现在将显示宽度划分为按钮所在的部分。因为我只是讨厌数学,所以我总是对 X/Y 的东西感到害怕,但现在我对它很好。再次感谢
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2020-10-28
    • 2021-05-02
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多