Java 而循环不';我不能在安卓上工作
我需要显示延迟1秒的数字。如果没有while循环,它可以工作,但是一旦我添加while循环,它就会在while循环结束后打印值。有人能在这方面帮忙吗Java 而循环不';我不能在安卓上工作,java,android,Java,Android,我需要显示延迟1秒的数字。如果没有while循环,它可以工作,但是一旦我添加while循环,它就会在while循环结束后打印值。有人能在这方面帮忙吗 int state = 0; int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
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++);
}
}
});
}
int state=0;
整数计数=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.b_main);
textView=(textView)findViewById(R.id.text\u main);
listView=(listView)findViewById(R.id.t_main);
arrayList=新的arrayList();
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(状态==0){
状态=1;
试一试{
while(count永远不要在UI线程上调用Thread.sleep()
。您需要将代码放在单独的线程中,然后使用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();
}
if(状态==0){
状态=1;
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
while(count不要使用Thread.sleep()
。使用处理程序强制执行所需的延迟
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** )
当您执行与时间相关的操作时,更好的方法是使用CountDownTimer
。在主线程上使用Thread.sleep()
是不好的,因为当线程处于睡眠状态时,您的Ui将被卡住
下面是一个如何实现它的示例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();
}
您从不在代码中增加count
。。。。