安卓输入框增加倒计时 CountDownTimer

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/H517604180/article/details/82423642

安卓中的倒计时功能,在倒计时中,修改剩余时间;倒计时结束后,可以做某些操作(本例中是将输入框关掉)。

倒计时类的用法如下:

	...
final View longinDialogView = layoutInflater.inflate(R.layout.cert_sign, null);
	...
CountDownTimer timer = new CountDownTimer(60*1000, 1000) {//参数表示,倒计时时间60s,每次间隔(倒计时频率)1s,单位是ms。
  @Override
  public void onTick(long millisUntilFinished) {//每经过一个时间间隔调用方法,即1s执行一次,Tick英文为发出滴答声
      String sec = millisUntilFinished/1000+"";
       if(sec.length()==1){
             sec = "0" + sec;
            }
         counterDown.setText("请在 "+sec+" 秒内输入");
         }
  @Override
   public void onFinish() {//倒计时结束调用方法
       dialog.cancel();
    }
 }.start();

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:weightSum="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.09"
        android:text="\r\n   验证证书密码"
        android:textSize="21sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#e6444a"

        />

    <TextView
        android:id="@+id/请设置密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.03"
        android:lineSpacingExtra="8sp"
        android:textAlignment="textStart"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密码:"
            android:textAlignment="center"
            android:textSize="19sp" />

        <EditText
            android:id="@+id/certPin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.46"
            android:ems="10"
            android:inputType="textPassword"
            android:maxLength="20"
            android:theme="@style/MyEditText" />

    </LinearLayout>

    <TextView
        android:id="@+id/counterdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.09"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp" />

</LinearLayout>

效果图:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/H517604180/article/details/82423642
今日推荐