我有几个按钮可以加减。如果计数器降到0或者低于游戏结束时我想要一个显示器弹出。我有winGame();在每个按钮clicklistener下设置功能以检查计数器总数。 **我遇到的问题是,无论计数器的价值是什么,它总是显示文本框说游戏结束。每次单击按钮添加或减去总自动时都会调出texbox。另外值得注意的是,当应用程序启动时,计数器从20开始。 这是代码。
void winGame() {
if (counter1 <= 0 || counter <=0 );
String text = "Game Over";
// Build a dialog box and with the result string and a single button
AlertDialog.Builder game = new AlertDialog.Builder(this);
game.setMessage(text).setCancelable(false)
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// do things when the user clicks ok.
}
});
AlertDialog alert = game.create();
// Show the dialog box.
alert.show();
}
答案 0 :(得分:4)
一个微妙的小虫......
if (counter1 <= 0 || counter <=0 );
应该是
if (counter1 <= 0 || counter <=0 )
或者这就是你想要的?
void winGame() {
if (counter1 <= 0 || counter <=0 )
{
String text = "Game Over";
// Build a dialog box and with the result string and a single button
AlertDialog.Builder game = new AlertDialog.Builder(this);
game.setMessage(text).setCancelable(false)
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// do things when the user clicks ok.
}
});
AlertDialog alert = game.create();
// Show the dialog box.
alert.show();
}
}