倒计时结束时显示倒计时并更改为其他活动。

时间:2014-03-27 13:44:06

标签: java android timer

我的活动在10秒后打开另一项活动。我知道如何在一段时间后打开另一个活动。但我想做的也是每秒计数,所以我可以将它显示给用户。我怎样才能做到这一点?在我的活动中,我有一个textview,我想在其上显示倒计时。

public class RestingActivity extends Activity{
    private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resting);

    mHandler.postDelayed(mLaunchTask, 10000);
}

public void skipRestTime(View view) 
{
    finish();
}


private Runnable mLaunchTask = new Runnable() {
    public void run() {
        finish(); // Closing the activity
    }
 };

}

2 个答案:

答案 0 :(得分:2)

请检查我的答案。它对我来说很好。希望它对你有所帮助。请尝试让我知道。感谢

<强> activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:background="#0090FF"
    android:padding="10dp"
    android:keepScreenOn="true" >

    <TextView
    android:id="@+id/txtCountDown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textColor="#fff" />

</RelativeLayout>

<强> MainActivity.java:

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static TextView txtCountDown;
    private static CountDownTimer countDownTimer = null;
        private static final long startTime = 120 * 1000;
        private static final long interval = 1 * 1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtCountDown = (TextView) findViewById(R.id.txtCountDown);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        if(txtCountDown!=null){
            txtCountDown.setText(String.valueOf(startTime / 1000));
        }
        countDownTimer.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class MyCountDownTimer extends CountDownTimer {

      public MyCountDownTimer(long startTime, long interval) {
          super(startTime, interval);
      }

      @Override
      public void onFinish() {
          if(txtCountDown!=null){
            txtCountDown.setText("Time's up!");
          }
      }

      @Override
      public void onTick(long millisUntilFinished) {
          if(txtCountDown!=null){
            txtCountDown.setText("" + millisUntilFinished / 1000);
          }
      }
   }
}

答案 1 :(得分:1)

您可以使用CountDownAnimation class。您只需要传递将显示倒计时的TextView并设置CountDownListener。然后,实施onCountDownEnd来更改活动。

我使用Handler,这比CountDownTimer准确得多。当我实现CountDownAnimation时,我对这些类进行了时间测试。