【问题标题】:Change Button Text on Android在 Android 上更改按钮文本
【发布时间】:2011-10-18 10:11:57
【问题描述】:

我有一个简单的 android 应用程序,带有一个带有文本 HELLO 的按钮。 如果我在 10 秒后不按此按钮,我希望文本等待。 有人能帮我吗? 谢谢

【问题讨论】:

  • 因为你必须使用定时器......通过使用定时器你可以设置按钮文本等待 10 秒后.....

标签: android button timer


【解决方案1】:

使用此代码

handler = new Handler(); 
handler.postDelayed(changeFunction(), 10*1000);

上面写在onCreate()中

private Runnable changeFunction(){ 
      t = new Timer();
      tt = new TimerTask() {              
          public void run() { 
              handler.postDelayed(changeFunction(), 10*1000);
              button.setText("WAIT");
          }          
      };          
    return tt;
    }

【讨论】:

    【解决方案2】:

    检查这个定时器任务 10 秒 ...button.setText("Wait...");

    http://developer.android.com/resources/articles/timed-ui-updates.html

    【讨论】:

      【解决方案3】:

      这应该可以工作

      Timer buttonTimer                   =   new Timer();
      final Runnable Timer_Tick = new Runnable() {
          public void run() {
          button.setText("WAIT");
      }
      };
      
      buttonTimer.schedule(new TimerTask(){
          @Override
          public void run(){
              runOnUiThread(Timer_Tick);
          }
      },10000);
      

      【讨论】:

        【解决方案4】:

        您可以使用定时处理程序消息。

        Button b;
        boolean notPressed;
        b.postDelayed(new Runnable() {
        
            @Override
            public void run() {
                if(notPressed){
                    b.setText("Wait");
                }
            }
        }, 10000);
        

        【讨论】:

          【解决方案5】:
          Button b;
          boolean notPressed;
          b.postDelayed(new Runnable() {
          
              @Override
              public void run() {
                  if(notPressed){
                      b.setText("sexy");
                  }
              }
          }, 10000);
          

          【讨论】:

            猜你喜欢
            • 2019-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-17
            • 2015-12-17
            • 1970-01-01
            • 2020-05-08
            相关资源
            最近更新 更多