简体   繁体   English

如何在满足特定条件后停止按钮取值

[英]How to stop a button to take the value after certain condition meet

I have made a simple tic tac toe game app using java.it is working as required, but after wining form one side it is keep taking the value.我使用 java 制作了一个简单的井字游戏应用程序。它按要求工作,但在赢得一方之后,它继续获得价值。 How to stop the execution after certain condition meet.如何在满足某些条件后停止执行。 If this is one of the condition of declaring the winner如果这是宣布获胜者的条件之一

  else if (b1.equals(b4) && b4.equals(b7) && !b4.equals("")) {
                    Toast.makeText(this, "Winner is " + b1, Toast.LENGTH_LONG).show();

then what should do to stop taking the input from all the button until restart?那么应该怎么做才能停止从所有按钮获取输入直到重新启动?

You can always disable the buttons after that condition is met:满足该条件后,您始终可以禁用按钮:

button.setEnabled(false)

Since you're saying you don't want the user to continue the game after it ended, you can also create an alert dialog, telling the game has ended, and when the user clicks "Ok" it restarts the game:既然你说你不希望用户在游戏结束后继续游戏,你也可以创建一个警告对话框,告诉游戏已经结束,当用户点击“确定”时它会重新启动游戏:

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  
        // Set the message show for the Alert time
        builder.setMessage("Winner is (insert winner name)");
  
        // Set Alert Title
        builder.setTitle("Game Ended");
  
        // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain showing
        builder.setCancelable(false);
  
        // Set the positive button with Ok
        builder.setPositiveButton("Ok", (DialogInterface.OnClickListener) (dialog, which) -> {
            // When the user click yes button then restart the game
            restartGame(); //Create this function to restart the game, for example
        });
  
        // Create the Alert dialog
        AlertDialog alertDialog = builder.create();
        // Show the Alert Dialog box
        alertDialog.show();

i am not sure if i get your question but you can do it in while loop such as我不确定我是否得到你的问题,但你可以在 while 循环中完成,例如

while(condition){
 // take the value
 }

and you can just update the value of condition after it finished.你可以在它完成后更新条件的值。

You can set button.setEnabled(false) after your condition is satisfied.您可以在满足条件后设置 button.setEnabled(false)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果满足某些条件,如何在启动过程中退出spring app? - how to quit spring app during startup if certain condition meet? 如何在满足条件后停止值事件侦听器的侦听 - how to stop value event listener from listening after a condition is met 满足条件时停止两个线程 - Stop two threads when the condition is meet 如何停止程序直到满足某个条件? - How to stop the program until a certain condition is met? 如何在启动一定次数后或返回特定值时停止 ScheduledExecutorService? - How to stop ScheduledExecutorService after a certain number of starts or when a certain value returns? 满足特定条件时禁用按钮 - Disable the button when meet specific condition Java-如何遍历对象数组列表并将满足特定条件的元素添加到另一个对象数组列表 - Java - How to iterate through an objects arraylist and adding elements that meet a certain condition to another objects arraylist 按钮数量达到一定值后如何增加按钮的值? - How to increase value of a button after button number has reached certain value? 如何在Java中的一段时间后停止执行? - How to stop execution after a certain time in Java? 如何在一定时间后停止计时器? - How to stop the timer after certain time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM