在Android上同时按下两个按钮

时间:2012-02-15 12:56:40

标签: android button multi-touch

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

  • 我在Galaxy Nexus 4.0.3上测试了该应用,效果很好。
  • 刚刚在HTC Desire 2.3.7上测试过,并没有按预期工作。

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

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

由于

3 个答案:

答案 0 :(得分:0)

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

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

你可以在Android 4.0上按2个不同的按钮'这个'应该是一个错误,因为每个按钮都有一个onClickListener在同一个线程中运行,所以不能同时按下两个按钮... < / p>

答案 1 :(得分:0)

据我所知,HTC Desire(以及它的姐姐Nexus One)并不具备多点触控能力。它更像是假的,我想他们会告诉你一个&#34; square&#34;按下(不确定)。

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

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

答案 2 :(得分: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]);
    }
}