Change Button Text on Android

2019-06-07 16:27发布

问题:

I have a simple android app with a button who have the text HELLO. If i do not push this button after 10 seconds i want the text to be WAIT. Can someone help me? Thanks

回答1:

use this code

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

write above in onCreate()

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


回答2:

check this timer task for 10 seconds ...button.setText("Wait...");

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



回答3:

This should work

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:

You can use a timed handler message.

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);