很抱歉提出这样一个基本问题,实际上我需要在一定的时间间隔之后调用一个方法,这实际上是将文本分配给android中的textView,这应该会改变。所以请建议我最好的方法来做到这一点。 在期待中感谢你。
{
int splashTime=3000;
int waited = 0;
while(waited < splashTime)
{
try {
ds.open();
String quotes=ds.getRandomQuote();
textView.setText(quotes);
ds.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
waited+=100;
}
答案 0 :(得分:4)
您考虑过CountDownTimer
了吗?例如:
/**
* Anonymous inner class for CountdownTimer
*/
new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick
/**
* Handler of each tick.
* @param millisUntilFinished - millisecs until the end
*/
@Override
public void onTick(long millisUntilFinished) {
// Currently not needed
}
/**
* Listener for CountDownTimer when done.
*/
@Override
public void onFinish() {
ds.open();
String quotes=ds.getRandomQuote();
textView.setText(quotes);
ds.close();
}
}.start();
当然,你可以把它放在一个循环中。
答案 1 :(得分:1)
你可以使用Timer来延迟更新你的用户界面,如下所示:
long delayInMillis = 3000; // 3s
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// you need to update UI on UIThread
runOnUiThread(new Runnable() {
@Override
public void run() {
ds.open();
String quotes=ds.getRandomQuote();
textView.setText(quotes);
ds.close();
}
});
}
}, delayInMillis);
答案 2 :(得分:1)
使用处理程序并将其放在Runnable:
中int splashTime = 3000;
Handler handler = new Handler(activity.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
ds.open();
String quotes=ds.getRandomQuote();
textView.setText(quotes);
ds.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}, splashTime);