android_countDownTimer倒计时

countDownTimer实现获取手机号倒计时

简单写下布局

<LinearLayout
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_weight="2"
        android:text="手机号"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <EditText
        android:inputType="phone|number"
        android:maxLength="11"
        android:id="@+id/telephone"
        android:textSize="15sp"
        android:hint="请输入手机号"
        android:layout_weight="7"
        android:background="@null"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>
<View
    android:background="@color/gray"
    android:layout_width="match_parent"
    android:layout_height="1dp"></View>

<LinearLayout
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:layout_gravity="center_vertical"
    android:layout_weight="2"
    android:text="验证码"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_weight="7"
        android:layout_width="0dp"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
<EditText
    android:id="@+id/intoyzm"
    android:textSize="15sp"
    android:hint="请输入验证码"
    android:layout_weight="1"
    android:background="@null"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />
 <Button
     android:id="@+id/get_yzm"
     android:background="@drawable/login_message"
     android:textSize="10sp"
     android:text="获取验证码"
     android:textColor="@color/blue"
     android:layout_width="80dp"
     android:layout_height="25dp" />
    </LinearLayout>

代码实现

case R.id.get_yzm:
    String telephoneNUmber=ed_telephone.getText().toString().trim();
    if(!TextUtils.isEmpty(telephoneNUmber)) {
        if(telephoneNUmber.length()==11) {
            SMSSDK.getVerificationCode("86",telephoneNUmber);
            ed_intoYzm.requestFocus();
            new CountDownTimer(30000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    //防止计时过程中重复点击
                    btn_getYzm.setClickable(false);
                    btn_getYzm.setText("等待" + millisUntilFinished / 1000 + "秒");
                }

                @Override
                public void onFinish() {
                    btn_getYzm.setText("获取验证码");
                    btn_getYzm.setClickable(true);
                }
            }.start();
        }else{
            Toast.makeText(this,"手机号码输入错误",Toast.LENGTH_SHORT).show();
            ed_telephone.requestFocus();
        }
    }else{
        Toast.makeText(this,"手机号不能为空",Toast.LENGTH_SHORT).show();
        ed_telephone.requestFocus();
    }
    break;

猜你喜欢

转载自blog.csdn.net/iamchan/article/details/81214151
今日推荐