[英]While loop doesn't work on Android
I need to display numbers with a delay of 1 second.我需要以 1 秒的延迟显示数字。 Without the while loop it works but once I add the while loop it prints the value after the while loop ends.
如果没有 while 循环,它可以工作,但是一旦我添加了 while 循环,它就会在 while 循环结束后打印值。 Can someone help in this regard.
有人可以在这方面提供帮助。
int state = 0;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.b_main);
textView =(TextView) findViewById(R.id.text_main);
listView =(ListView) findViewById(R.id.t_main);
arrayList=new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(state==0){
state=1;
try {
while(count<10) {
textView.setText("Start " + count++);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
state=0;
textView.setText("Stop " + count++);
}
}
});
}
Never call Thread.sleep()
on the UI thread.永远不要在 UI 线程上调用
Thread.sleep()
。 You need to put the code in a separate thread, and post the result using runOnUiThread
:您需要将代码放在一个单独的线程中,并使用
runOnUiThread
发布结果:
if(state==0){
state=1;
new Thread(new Runnable() {
@Override
public void run() {
try {
while(count<10) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Start " + count++);
}
});
Thread.sleep(1000);
count++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
Don't use Thread.sleep()
.不要使用
Thread.sleep()
。 Use a handler to enforce the delay you want.使用处理程序来强制执行您想要的延迟。
final Handler handler = new Handler();
final int delay1 = 1000; // adjust as needed
handler.postDelayed(new Runnable() {
public void run() {
// Code Block 1. Code here will be executed after 1000 millisec
}
}, delay1);
// Code Block 2. Code here will be executed immediatelly ( before **Code Block 1** )
As you are doing operation related to time the better approach would be to use CountDownTimer
.当您进行与时间相关的操作时,更好的方法是使用
CountDownTimer
。 It is bad to use Thread.sleep()
on main thread because your Ui will get stuck when thread is sleeping在主线程上使用
Thread.sleep()
是不好的,因为当线程休眠时你的 Ui 会卡住
Here is an example on how to achieve it CountDownTimer
这是一个关于如何实现它的示例
CountDownTimer
int state = 0, count = 0;
TextView textView;
CountDownTimer timer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button=(Button)findViewById(R.id.b_main);
textView =(TextView) findViewById(R.id.text_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (state == 0) {
state = 1;
startTimer();
} else {
state = 0;
if (timer != null) {
timer.cancel();
}
textView.setText("Stop " + count++);
}
}
});
}
public void startTimer() {
timer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
count++;
textView.setText("Start " + count);
}
@Override
public void onFinish() {
//10 sec finished
}
};
timer.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.