Android 发送手机号验证码倒计时
程序员文章站
2022-03-15 13:29:42
.xml文件获取验证码倒计时通过TimeCount 计时器实现的,记得在倒计时过程中将按钮设置为禁用
.xml文件
获取验证码倒计时通过TimeCount 计时器实现的,记得在倒计时过程中将按钮设置为禁用
<LinearLayout
android:layout_marginTop="@dimen/dp_100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:paddingTop="@dimen/dp_50"
android:layout_marginRight="@dimen/dp_20"
android:paddingBottom="@dimen/dp_50"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_35"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_5"
android:background="@drawable/bk_register"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/dp_24"
android:layout_height="@dimen/dp_24"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dp_10"
android:src="@drawable/ic_phone_iphone" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_6"
android:background="@null"
android:textSize="@dimen/sp_16"
android:hint="请输入手机号码" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_35"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_5"
android:background="@drawable/bk_register">
<ImageView
android:layout_width="@dimen/dp_24"
android:layout_height="@dimen/dp_24"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_10"
android:src="@drawable/ic_lock_black" />
<EditText
android:textSize="@dimen/sp_16"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_40"
android:background="@null"
android:hint="请输入验证码" />
<TextView
android:id="@+id/tv_yzm"
android:textSize="@dimen/sp_16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_10"
android:textColor="#0a71b0"
android:text="获取验证码" />
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="@dimen/dp_35"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_5"
android:background="@drawable/bg_setting_button"
android:text="确认"
android:textColor="@color/white"
android:textSize="@dimen/sp_14" />
</LinearLayout>
.java文件
private TextView tvYzm;
private EditText etPhone;
private TimeCount time;
...
onCrate(Bundle savedInstanceState){
...
time = new TimeCount(60000, 1000);
tvYzm = findViewById(R.id.tv_yzm);
etPhone = findViewById(R.id.et_phone);
tvYzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(etPhone.getText().toString())) {
etPhone.setError("请先输入手机号");
return;
}
if (!isMobileNO(etPhone.getText().toString())) {
etPhone.setError("手机号格式不正确");
return;
}
time.start();//成功按钮发送验证码倒计时
}
});}
工具
//验证码倒计时
class TimeCount extends CountDownTimer {
public TimeCount(long millisinfuture, long countdowninterval) {
super(millisinfuture, countdowninterval);
}
@Override
public void onTick(long l) {
tvYzm.setClickable(false);
tvYzm.setText("(" + l / 1000 + ")s");
tvYzm.setTextColor(getResources().getColor(R.color.gray));
}
@Override
public void onFinish() {
tvYzm.setText("发送验证码");
tvYzm.setClickable(true);
}
}
//判断手机号码格式是否正确
private boolean isMobileNO(String mobiles) {
String telRegex = "^((1[3,5,7,8][0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
if (TextUtils.isEmpty(mobiles)) {
return false;
} else {
return mobiles.matches(telRegex);
}
}
本文地址:https://blog.csdn.net/weixin_45407364/article/details/110635958