【发布时间】:2018-03-02 12:05:33
【问题描述】:
我对 android 开发相当陌生,不知道如何对按钮按下进行双重检查。 我在 Android Studio 中工作,正在尝试创建一个简单的游戏并具有以下内容:在 onClick 方法中声明和实例化了两个按钮(按钮 1 和按钮 2)。 当按下按钮 1 时,计数器会增加,并且按钮上会显示一个数组中的特定值,其值为 0、10、50、100。 这部分就像一个魅力。我不能做的是创建一个有效的“决胜规则”,应该是这样的:
if (button1 value == 100 and button2 value == 100) or this can be done with the counter if (counter1 == 3 && counter2 == 3)
tiebreaker();
这是我需要做的:
public void tiebreaker(){
if (button1 is pressed){
button1.setText(String.valueOf("OK. One more!");
if (button1 is pressed (again){
resetCounters();
goForPlayer1(); //distinct method
} else if (button2 is pressed) {
//set both values back to 100
tiebreaker(); //calls itself in order to repeat until a button is pressed twice consecutivelly
} (else?)
if(button2 is presed){
//same code as above, just with the numbers 1 and 2 swapped
}
}
我已经尝试以不同的方式实现这种决胜局方法,但没有最终成功。我尝试使用if(button1.isPressed()) 作为条件,按下按钮时布尔值变为真并将其用作条件,使用带有button.equals("100") or whatever should be written 的按钮中的值,但没有。
另外,我尝试将 tiebreaker 作为第二个 onClick 方法并根据 switch (View.getId()) 进行检查,但这里的问题是该方法依赖于视图 (tiebreaker(View v)) 我不知道如何从不同的方法。
IMO 的主要问题是,如果在另一个 if 或另一个 switch/case 中的 switch/case 在按下按钮时不能使用(不过我可能是错的),但我不知道有任何其他方式来做到这一点。
我希望我没有遗漏任何事情并设法让自己清楚。 我感谢任何帮助我解决这个决胜规则的意见。谢谢
【问题讨论】:
标签: android if-statement button switch-statement