当我单击 activity 时,我在其中有 3 个文本视图。我想在间隔 2 秒后一个接一个地加载文本视图?
When I click on the activity I have 3 Text View in that. I want to load the Text View one after another like after a gap of 2 seconds?
我不确定,但我的研究让我得出了这一点。我想我需要创建 Thread 对象然后我可以使用 Thread.sleep(seconds);
但我不确定它如何与文本视图一起使用。
private void runThread() {
new Thread() {
public void run() {
int i = 0;
while (i++ < 2) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv1 = (TextView) findViewById(R.id.tvFirst);
TextView tv2 = (TextView) findViewById(R.id.tvSecond);
TextView tv3 = (TextView) findViewById(R.id.tvThird);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
这甚至没有响应。我知道我在这里做错了,但这就是我现在能想到的。任何帮助将不胜感激。
使用Handler.postDelayed。示例:
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv2 = (TextView) findViewById(R.id.tvSecond);
tv2.setText("I set this text after waiting for 2 seconds");
}
});
}
},2000);
这将在两秒后更新您的第二个 textView。你可以对其他人做几乎相同的事情。
我不确定,但我的研究让我得出了这一点。我想我需要创建 Thread 对象然后我可以使用 Thread.sleep(seconds);
但我不确定它如何与文本视图一起使用。
private void runThread() {
new Thread() {
public void run() {
int i = 0;
while (i++ < 2) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv1 = (TextView) findViewById(R.id.tvFirst);
TextView tv2 = (TextView) findViewById(R.id.tvSecond);
TextView tv3 = (TextView) findViewById(R.id.tvThird);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
这甚至没有响应。我知道我在这里做错了,但这就是我现在能想到的。任何帮助将不胜感激。
使用Handler.postDelayed。示例:
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv2 = (TextView) findViewById(R.id.tvSecond);
tv2.setText("I set this text after waiting for 2 seconds");
}
});
}
},2000);
这将在两秒后更新您的第二个 textView。你可以对其他人做几乎相同的事情。