onClick Multiple Buttons at once multitouch

て烟熏妆下的殇ゞ 提交于 2019-12-25 11:52: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.



来源:https://stackoverflow.com/questions/8716853/onclick-multiple-buttons-at-once-multitouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!