我正在尝试根据经过的时间执行一些更新 UI 的任务,以下是我的代码:
我的 OnCreate 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.textView);
startTime = System.nanoTime();
estimatedTime = (System.nanoTime() - startTime) / 1000000000;
tx.postInvalidate();
System.out.println(""+estimatedTime);
MainActivity.this.runOnUiThread(new Runnable(){
public void run() {
while (estimatedTime <= 100){
System.out.println(""+estimatedTime);
if(estimatedTime == 10){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Shri Ram Raksha Stotram");
System.out.println("Yay");
}
if(estimatedTime == 20){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Inatializing");
System.out.println("Yay");
}
if(estimatedTime == 30){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Preparing to install");
System.out.println("Yay");
}
if(estimatedTime == 40){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Installing");
System.out.println("Yay");
}
if(estimatedTime == 50){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Installed");
System.out.println("Yay");
}
if(estimatedTime == 60){
TextView tx = (TextView)findViewById(R.id.textView);
tx.setText("Unzipping packages...");
System.out.println("Yay");
}
estimatedTime = (System.nanoTime() - startTime) / 1000000000;
}
}
});
}
以上所有代码都在 OnCreate 方法上,但是即使在实现 RunonUI 线程之后我也无法更新 textview,我在 textview 上得到的唯一结果是最后一个 if 条件“解压缩包”。我哪里错了?