【问题标题】:Check if a button is pressed and then check again检查是否按下按钮,然后再次检查
【发布时间】: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


    【解决方案1】:

    大概是这样的……

    whenButtonAIsPressed() {
    
        do some stuff
        possiblyRunTiebreakerMaterial()
    }
    
    whenButtonBIsPressed() {
    
        do some stuff
        possiblyRunTiebreakerMaterial()
    }
    

    请注意,您始终运行possiblyRunTiebreakerMaterial(),而您始终结束时运行它 进行所有其他处理。

    这是possiblyRunTiebreakerMaterial() 函数的性质...

    possiblyRunTiebreakerMaterial() {
    
        // there are some conditions, which mean you DO want
        // to run the tie breaker:
        do some processing, such as
        value of A = blah blah
        value of B = blah blah
    
        // now have a boolean, and figure it out
        boolean  shouldWeRunTheTieBreaker
    
        processing
        processing
        shouldWeRunTheTieBreaker = blah
    
        and now finally:
        is shouldWeRunTheTieBreaker == true { tiebreaker() }
    }
    

    然后有你的tiebreaker() 函数。

    您真正需要的是状态机,但暂时不用担心。

    你在这里寻找的一般“编程技巧”是你必须做这些事情:

    1. 始终进行处理
    2. 那么,只有之后
    3. 所有情况下,检查IF您需要运行决胜局
    4. 如果需要运行

    这就是模式。

    【讨论】:

    • 感谢您的回答。我遇到的问题不是检查是否应该调用 tiebreaker,而是检查方法本身的内部。目前,我不知道要在决胜局中写什么才能完成我上面描述的操作。
    【解决方案2】:

    很遗憾,我没有可以投票的评分。 我设法解决了这个问题。我为 tiebreaker 创建了一个 boolean 而不是一个函数,它看起来像:

    if (p1 = 100 || p2 == 100)
       tiebreak = true;
    if (p1 == 100 && tiebreaker){
       p1 == OK;
       tiebreaker = false;
    } else //same thing for p2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2020-06-28
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多