CountDownTimer倒计时

直接上代码
public class MainActivity extends Activity {

    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = ((TextView) findViewById(R.id.text));
        timer.start();

    }
    private CountDownTimer timer=new CountDownTimer(6000,1000) {
        @Override
        public void onTick(long l) {
            text.setText((l/1000)+"秒后可重发");
        }

        @Override
        public void onFinish() {
            text.setText("获取验证码");

        }
    };
}
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
 

你可能感兴趣的:(CountDownTimer倒计时)