onClick Multiple Buttons at once multitouch

2019-09-18 00:12发布

问题:

Im programming a little game where two Players have to click some Buttons it works pretty good if there is only one player, but when the other player is also playing his part of the game then the Buttons dont do anything.

How can i enable Multitouch so that 2 Buttons can get clicked at once ?

EDIT: Here is some Code:

Layout XML

<ImageButton
    android:id="@+id/game1_player2"
    [...]
    android:background="@android:color/transparent"
    android:src="@drawable/player2_countdown_1" 
    android:onClick="player2method"/>

Iny My Java extends activity File:

public void player1method (View v)
{
    if(buttonzahl == player2_nextbutton)
    {
        Toast.makeText(getApplicationContext(), "player1 won", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "player2 won", Toast.LENGTH_LONG).show();
    }
}

public void player2method(View v)
{
    if(buttonzahl == player2_nextbutton)
    {
        Toast.makeText(getApplicationContext(), "player2 won", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "player1 won", Toast.LENGTH_LONG).show();
    }
}

If Player one clicks his buttons all is fine but if the other player also clicks no button is activated :(

回答1:

Multiple button presses for Android 2.x

Click two buttons at the same time in Android

so from the first posted link it seems like you need to override the touch events not just the onclick

the second post talks about versions

if neither of these help, please post some code

few more links due to comments below

Android multitouch: ACTION_UP not always called?

Android - OnTouch Do Work

getAction() gives only ACTION_DOWN



回答2:

You can do it easily by running a separate thread that would listen to TouchEvents and Updates the UI with the help of Handler. Since your UI is never stuck and background thread will always listen for simultaneous touch events.