TextView Timer麻烦

时间:2013-12-02 15:37:55

标签: android timer textview

我是编写代码世界的新手,我需要TextView的帮助。我希望显示的android时间在屏幕上倒计时,例如从10到1.在分钟它显示第一个数字,但它不会改变。任何帮助都会受到很大的限制。

private final Handler mHandler = new Handler();
private TextView mTest;
private long startTime = 10 * 1000;

private Runnable mTask = new Runnable()
{
  public void run()
  {
     mTest.setText("Sorry. Times up!");

  }//run

};//mTask

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mTest = (TextView) findViewById(R.id.timer);
  mTest.setText(mTest.getText() + String.valueOf(startTime / 1000));
  mHandler.postDelayed(mTask, 10000);

}//onCreate

@Override
protected void onDestroy() {
  super.onDestroy();
  mHandler.removeCallbacks(mTask);

}//onDestroy

1 个答案:

答案 0 :(得分:1)

试试这个:

TextView mTest;
private final Handler mHandler = new Handler();
private int startTime = 10;
private Runnable mTask = new Runnable()
{
    public void run()
    {
        if(startTime > 0)
        {
            mTest.setText(String.valueOf(startTime));
            startTime--;
            mHandler.postDelayed(mTask, 1000);
        }
        else
        {
            mTest.setText("Sorry. Times up!");              
        }
    }
};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTest = (TextView) findViewById(R.id.timer);
    mHandler.post(mTask);
}