我目前正在开发一个android应用程序。它需要定期更新 textview 值。
例如,我想每秒将值增加 10。我尝试使用以下代码,但它不能正常工作:textview 仅在增量完成后更新
package com.example.stack1;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onResume(){
super.onResume();
TextView output=(TextView) findViewById(R.id.output);
output.setText(String.valueOf(0));
System.out.println(0);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
output.setText(String.valueOf(10));
System.out.println(10);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
output.setText(String.valueOf(20));
System.out.println(20);
}
}
output
是 main.xml 文件中的一个文本视图。此文件仅包含此对象。
注意 - textview 中的预期输出为“0”,10 秒后为“10”,20 秒后为“20”。但是,使用此代码,输出在 20 seconds 之前为空白,然后出现“20”。