我想在 textView (Live) 中显示一个线程的计时器
I would like to show the timer of a thread in textView (Live)
我在一个线程中有一个计时器,在移动到一个新的 activity 之前等待大约 20 秒,我正在寻找的是在这段时间内在 textView 中显示减少或增加的时间。
这是我的代码:
Thread timer = new Thread() {
public void run() {
try {
sleep(ie);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent i = new Intent(Activity.this, Menu.class);
if (ie == 20000) {
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
}
}
};
timer.start();
感谢您的协助
尝试使用 CountDownTimer 而不是线程:
CountDownTimer count = new CountDownTimer(20000, 1000)
{
int counter = 20;
@Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
counter--;
textView.setText(String.valueOf(counter));
}
@Override
public void onFinish()
{
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
};
count.start();
Camilo Sacanamboy 的回答是正确的。如果你想用线程来做到这一点,一个解决方案可能是这样的:
final TextView status; //Get your textView here
Thread timer = new Thread() {
public void run() {
int time = 20000;
try {
while(time > 0){
time -= 200; //Or whatever you might like
final int currentTime = time;
getActivity().runOnUiThread(new Runnable() { //Don't update Views on the Main Thread
@Override
public void run() {
status.setText("Time remaining: " + currentTime / 1000 + " seconds");
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent i = new Intent(Activity.this, Menu.class);
if (ie == 20000) {
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
}
}
};
timer.start();
我在一个线程中有一个计时器,在移动到一个新的 activity 之前等待大约 20 秒,我正在寻找的是在这段时间内在 textView 中显示减少或增加的时间。 这是我的代码:
Thread timer = new Thread() {
public void run() {
try {
sleep(ie);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent i = new Intent(Activity.this, Menu.class);
if (ie == 20000) {
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
}
}
};
timer.start();
感谢您的协助
尝试使用 CountDownTimer 而不是线程:
CountDownTimer count = new CountDownTimer(20000, 1000)
{
int counter = 20;
@Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
counter--;
textView.setText(String.valueOf(counter));
}
@Override
public void onFinish()
{
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
};
count.start();
Camilo Sacanamboy 的回答是正确的。如果你想用线程来做到这一点,一个解决方案可能是这样的:
final TextView status; //Get your textView here
Thread timer = new Thread() {
public void run() {
int time = 20000;
try {
while(time > 0){
time -= 200; //Or whatever you might like
final int currentTime = time;
getActivity().runOnUiThread(new Runnable() { //Don't update Views on the Main Thread
@Override
public void run() {
status.setText("Time remaining: " + currentTime / 1000 + " seconds");
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent i = new Intent(Activity.this, Menu.class);
if (ie == 20000) {
startActivity(i);
overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
}
}
};
timer.start();