0
我是Android的初学者。我想随着时间改变我的文本视图。 我的活动持续5秒钟,我想显示倒计时的时间。 我试过使用线程计时器,但它不工作。 只需我想显示像在Android中动态更改TextView
在起点的数字:5, 后的第二:4, 2秒钟后:3, ,,:2, ,,:1,
请建议我如何编码。
我是Android的初学者。我想随着时间改变我的文本视图。 我的活动持续5秒钟,我想显示倒计时的时间。 我试过使用线程计时器,但它不工作。 只需我想显示像在Android中动态更改TextView
在起点的数字:5, 后的第二:4, 2秒钟后:3, ,,:2, ,,:1,
请建议我如何编码。
使用CountDownTimer:
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtCount = (TextView) findViewById(R.id.txtCount);
final int secs = 5;
new CountDownTimer((secs +1) * 1000, 1000) // Wait 5 secs, tick every 1 sec
{
@Override
public final void onTick(final long millisUntilFinished)
{
txtCount.setText("" + (int) (millisUntilFinished * .001f));
}
@Override
public final void onFinish()
{
txtCount.setText("GO!");
}
}.start();
}