在Android上同时按下两个按钮

问题描述 投票:0回答:3

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

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

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

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

谢谢

android button multi-touch
3个回答
0
投票

据我所知,HTC Desire(以及它的妹妹Nexus One)并不是真正的多点触控功能。这更像是假的,我认为它们会告诉你一个被按下(不确定)的“方形”。

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

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


0
投票

不要使用两个不同的按钮,你应该在你的视图上有一个布局,并在这个视图的“区域”中定义,两个玩家都可以按...

事实上,你应该做的是使用你的Android设备的多点触控功能(如捏缩放等),但这次它将用于2个不同的手。

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


0
投票
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]);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.