[英]How can i pause program execution in the scenario given in description?
I am making an android app. 我正在制作一个Android应用程序。 Here's what I want: User clicks a button, button text changes to "xyz", and then the program waits for 1 second and then the button text changes to "abc".If I use Thread.sleep(1000) then the program does stop for a second but the button text doesn't change to "xyz" before the program goes to sleep.
这就是我想要的:用户单击一个按钮,按钮文本更改为“xyz”,然后程序等待1秒钟,然后按钮文本变为“abc”。如果我使用Thread.sleep(1000),那么程序会执行停止一秒钟但按钮文本在程序进入睡眠状态之前不会更改为“xyz”。
You can use Handler
to achieve this. 您可以使用
Handler
来实现此目的。 Using postdelayed
method you can do this. 使用
postdelayed
方法,您可以执行此操作。 Below is the code to do this 以下是执行此操作的代码
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setText("xyz"); // text changed to xyz
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button.setText("abc"); //text changed to abc after 1 second.
}
},1000);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.