【问题标题】:Two button pressed at the same time on Android在Android上同时按下两个按钮
【发布时间】:2012-02-15 12:56:40
【问题描述】:

我是 Android 编程新手,我编写了一个简单的游戏,其中两个玩家必须按下自己的按钮才能达到目标。有什么问题?

  • 我已经在 Galaxy Nexus 4.0.3 上测试了该应用程序并且运行良好。
  • 刚刚在 HTC Desire 2.3.7 上进行了测试,但没有按预期工作。

但在 Galaxy Nexus 上,2 位玩家可以同时按下按钮,而在 HTC Desire 上则不能。

这两款设备都具有多点触控功能。

谢谢

【问题讨论】:

  • 请注意,由于使用了传感器,Desire 的多点触控有些受限。有一些samples on YouTube。基本上,如果按钮位于相同的 X 或 Y 坐标上,则可能会混淆。您可以通过将按钮放在左上角和右下角或类似位置来解决此问题。
  • 所以这不是 Cehm 所说的 Android 4.0 的错误。我唯一能做的就是提醒用户 2vs2 模式仅适用于多点触控(真正的多点触控)设备……我说的对吗?
  • 要么这个,要么按照我的描述尝试解决。也许提供一个默认为故障安全的选项。当 Desire 是新的时,我有一个 SNES 模拟器,它有那个选项。不过似乎被撤出市场。如果你有心情,你可以看看其他人是如何解决它的。在市场上尝试其中一些应用程序,您可能会发现类似的东西(因为理论上几乎所有 NES 模拟器都应该遇到类似的问题) - 但是是的,毕竟这正是 android 赶上的原因真正多点触控的东西这么晚了。
  • 现在该怎样感谢你? :) 我无法选择你的答案...
  • 嗯,Luminger 说了几乎一样的话,所以请给他点赞。 :)

标签: android button multi-touch


【解决方案1】:

据我所知,HTC Desire(也是它的姊妹款 Nexus One)并没有真正的多点触控功能。这更像是一个假的,我想他们告诉你一个被按下的“正方形”(不确定)。

市场上有一些程序能够可视化设备识别的内容,您应该在两台设备上安装其中一个,然后以相同的多点触控方式触摸它们时查看差异在哪里。

网上也有更多关于它的资源,众所周知。

【讨论】:

  • 我在 Desire 中测试了指针位置(在开发工具应用程序中),它显示了两个压力点。我可以使用其他应用程序,例如钢琴 ecc,并且我始终可以同时按下最多 2 个按钮。
  • 确认 HTC Desire Z (Vision) 最多可以同时处理 4 个触摸 - 但在 WebViews 上只能处理 1 个。如果你需要 webviews 上的多点触控(即你的游戏是一个 HTML5 应用程序),你需要 s.th.喜欢github.com/Philzen/WebView-MultiTouch-Polyfill
【解决方案2】:

您应该在视图上进行布局并定义,而不是使用两个不同的按钮,并在此视图中定义两个玩家都可以按下的“区域”...

事实上,你应该做的是使用你的安卓设备的多点触控功能(比如捏缩放等),但这次它会被两只不同的手使用。

您可以在 Android 4.0 上按下 2 个不同按钮的事实“应该”是一个错误,不能同时按下两个按钮,因为每个按钮都有一个 onClickListener 在同一个线程中运行...

【讨论】:

    【解决方案3】:
    package whaever.package.used;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    
    // Modify this code as you wish.
    // See if it works for you.
    // I was looking for the same thing (press buttons and assync operation) on
    // stackoverflow.com ...
    // That is how I got to your question ... VERY LATE indeed (sorry, I am not
    // working very much with Android, sorry for the HUDGE delay)
    // Yet after reading the answers and after solving it for my particular case,
    // I decided to provide this answer, maybe it is usefull and will help someone.
    // Please be carefull as in my particular case I have much more code going
    // with it. !!! This is only a model !!!
    
    public class MainActivity extends AppCompatActivity {
    
        private final long butId_00_Down_Moment[] = new long[1];
        private final long butId_00_Up_Moment[] = new long[1];
        private final long butId_00_Duration[] = new long[1];
    
        private final long butId_01_Down_Moment[] = new long[1];
        private final long butId_01_Up_Moment[] = new long[1];
        private final long butId_01_Duration[] = new long[1];
    
        @SuppressLint("ClickableViewAccessibility")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final Button butId_00 = (Button) findViewById(R.id.but_id00);
            butId_00.setOnTouchListener (new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            butId_00_Down_Moment[0] = System.currentTimeMillis();
                            butId_00_Duration[0] = 0;
                            MainActivity.this.findViewById(R.id.but_id00).performClick();
                            butId00_OnDown(view);
                            return true;
                        }
                        case MotionEvent.ACTION_UP: {
                            butId_00_Up_Moment[0] = System.currentTimeMillis();
                            butId_00_Duration[0] = butId_00_Up_Moment[0] - butId_00_Down_Moment[0];
                            MainActivity.this.findViewById(R.id.but_id00).performClick();
                            butId00_OnUp(view);
                            return true;
                        }
                    }
                    return false;
                }
            });
            butId_00.setOnClickListener(new View.OnClickListener () {
                @Override /**/ public void onClick (View view) { butId00_OnClick(view); }
            });
    
            final Button butId_01 = (Button) findViewById(R.id.but_id01);
            butId_01.setOnTouchListener (new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            butId_01_Down_Moment[0] = System.currentTimeMillis();
                            butId_01_Duration[0] = 0;
                            MainActivity.this.findViewById(R.id.but_id01).performClick();
                            butId01_OnDown(view);
                            return true;
                        }
                        case MotionEvent.ACTION_UP: {
                            butId_01_Up_Moment[0] = System.currentTimeMillis();
                            butId_01_Duration[0] = butId_01_Up_Moment[0] - butId_01_Down_Moment[0];
                            MainActivity.this.findViewById(R.id.but_id01).performClick();
                            butId01_OnUp(view);
                            return true;
                        }
                    }
                    return false;
                }
            });
            butId_01.setOnClickListener(new View.OnClickListener () {
                @Override /**/ public void onClick (View view) { butId01_OnClick(view); }
            });
        }
    
        // For the first button "butId_00"
        private void butId00_OnDown(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId00_OnDown" + butId_00_Down_Moment[0]);
        }
        private void butId00_OnClick(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId00_OnClick" + butId_00_Duration[0]);
        }
        private void butId00_OnUp(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId00_OnUp" + butId_00_Up_Moment[0]);
        }
    
        // For the second button "butId_01"
        private void butId01_OnDown(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId01_OnDown" + butId_01_Down_Moment[0]);
        }
        private void butId01_OnClick(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId01_OnClick" + butId_01_Duration[0]);
        }
        private void butId01_OnUp(View view) {
            // TODO your code here
            Logger.getGlobal().info("butId01_OnUp" + butId_01_Up_Moment[0]);
        }
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:stackoverflow.com/help/how-to-answer。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    相关资源
    最近更新 更多