我即将完成我的 Simon Says 游戏,但我还有一个关键问题。
在我的按钮显示给播放器后,我必须等待(根据级别,例如,如果向用户显示 5 个按钮,我必须等待用户点击 5 个按钮)。我在互联网上搜索过,但唯一的答案是“你不能卡住 android 应用程序......等等等等”。
这是我的代码:
public boolean playerTurn(){
//Enable buttons
buttonUp.setClickable(true);
buttonDown.setClickable(true);
buttonRight.setClickable(true);
buttonLeft.setClickable(true);
/*
Wait for a button clicks depends on the level, but HOW?
e.g, if the level is 5, I have to wait for 5 button clicks
and after it, I can continue to run the code
*/
/*Check if the user typed the correct order
*If pressed the correct order
*return true;
*else
*return false
*/
}
最佳答案
对每个按钮做这样的事情:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
buttonsPressedCount++;
if (buttonsPressedCount >= 5) {
// Do whatever you wanted to do
}
}
});
这样,每个按钮都会监听点击并更新一个全局计数器。最后当你的全局计数器大于 5 时,你就可以继续了。该应用程序永远不会“暂停”,它只会在单击五个按钮后移动到下一部分。
关于android - 我如何等待android中的按钮点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398417/