I want my app to wait for a moment before changing the button text. I tried different things (wait(), Thread.sleep) but since I want the waiting time to be variable, I decided to make use of CountDownTimer. See the code below:
public void onClick(View buttonClicked) { if (i == 0) { button.setText("Wait"); waitingTime = (long) (Math.random() * 1000 + 10000); new countdown(waitingTime, 1000); time1 = System.currentTimeMillis(); button.setText("Press"); i++;
For some reason this doesn´t work, and the program does not wait at all. Can anyone please help me? I did not fill the CountDownTimer with anything since I figured that it should only wait for a couple of seconds.
Thread is also variable:
try {
Thread.sleep(1000);
} catch (Exception e) {
}
try this code
@Override
public void onClick(View view) {
if(i==0) {
YourActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
txtView.setText("please wait...");
}
});
while(counter < 1000) {
try {
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
counter +=100;
}
txtView.setText("press");
i++;
}
}
Increase the thread timing according to ur needs. And if needed write this code in the function where you are passing the sleep value
EDIT
mHandler = new Handler(); btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
btn.setText("wait");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
btn.setText("press");
}
}, 1500);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.