【问题标题】:How to assure that a button is only clicked a "n" times?! android studio如何确保一个按钮只被点击“n”次?!安卓工作室
【发布时间】:2019-04-22 08:16:27
【问题描述】:

这是一个游戏,这个游戏有一定的回合数,我只想点击玩家选择的回合数。

这是我的函数,它保存输入字符串中的最后两个单词,并在文本字段上显示,我只想运行这个函数“introndas”的数量...我尝试添加代码的最后一部分使用“next.setEnable(False)”...但什么也没做... 谢谢大家

public void onClick (View v){

    edText1=findViewById(R.id.txtatual);
    int introndas = Integer.parseInt(nrondas);
    String aux=guardatexto();
    String str[] = aux.split(" ");
    int lenghtofstr = str.length;

    if(lenghtofstr >=2){
    lword=str[lenghtofstr-1];
    pword=str[lenghtofstr-2];
    String wordstoshow=pword+" "+ lword;
    ltextview.setText(wordstoshow);

    FinalTexto=FinalTexto+" " + aux;}
    edText1.setText("");
    currentnumber++;

    if (currentnumber == introndas)
        next.setEnabled(false);
    else
        currentnumber = currentnumber + 1;

}

【问题讨论】:

    标签: java android android-studio button


    【解决方案1】:

    首先在您的Activity(或Fragment)中创建一个整数字段,用于存储按下该按钮的编号。说private int timesClicked = 0;

    然后在onClick() 检查这个计数器是否达到了你想要的限制。可能是

    int timesClicked = 0;
    
    public void onClick (View v){
      if(++timesClicked <= roundsSelected){
        // The limit is not reached yet...
      }else {
        // Player clicked button more times than the rounds selected.
        // Write logic here  
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2017-02-09
      相关资源
      最近更新 更多