繁体   English   中英

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

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

我使用 java 制作了一个简单的井字游戏应用程序。它按要求工作,但在赢得一方之后,它继续获得价值。 如何在满足某些条件后停止执行。 如果这是宣布获胜者的条件之一

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

那么应该怎么做才能停止从所有按钮获取输入直到重新启动?

满足该条件后,您始终可以禁用按钮:

button.setEnabled(false)

既然你说你不希望用户在游戏结束后继续游戏,你也可以创建一个警告对话框,告诉游戏已经结束,当用户点击“确定”时它会重新启动游戏:

        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();

我不确定我是否得到你的问题,但你可以在 while 循环中完成,例如

while(condition){
 // take the value
 }

你可以在它完成后更新条件的值。

您可以在满足条件后设置 button.setEnabled(false)。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM