Я пытаюсь сделать некоторые задачи обновления пользовательского интерфейса на основе истекшего времени после мой код:Run на обновления UI нить только последний желаемый результат (Android)
Мой 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 я не могу обновить текстовое представление, единственным результатом, который я получаю в текстовом виде, является последнее, если условие «Распаковывать пакеты». Где я иду не так?
каждый раз, когда вы инициализация и SetText. если вам нужно добавить текст в textview, используйте append вместо settext – Raghunandan
Не имеет смысла вызывать runOnUiThread() внутри onCreate(), так как сама onCreate() работает в потоке пользовательского интерфейса. – Egor
Но если я его не использую, TextView не будет обновлять? Как отделить код от onCreate? Я имею в виду, как мне его разветвить, извиняюсь за то, что ты запутался, но я не могу объяснить это лучше! –