Activity:
TimeCountUtil mTimeCountUtil =new TimeCountUtil(forget_verification_text, 1000*60, 1000);
mTimeCountUtil.start();
XML:
<Button
android:id="@+id/register_verification_text"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:gravity="center"
android:textSize="15dp"
android:background="@drawable/selector_button"
android:textColor="@color/main_tv_color"
android:layout_gravity="center_vertical"
android:text="获取验证码" />
Drawable目录下:
selector_button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_button_type1" android:state_enabled="true" />
<item android:drawable="@drawable/shape_button_type2" android:state_enabled="false" />
</selector>
shape_button_type2:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFDDDDDD"/>
<corners android:radius="15dp"/>
<stroke android:width="1px" android:color="#FFDDDDDD" />
</shape>
shape_button_type1:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners android:radius="15dp"/>
<stroke android:width="0.7dp" android:color="@color/text_gary_color" />
</shape>
工具类:
/**
* auther Android
* Created by on 2019/4/25.
* Describe :
*/
public class TimeCountUtil extends CountDownTimer{
private ButtonmButton;
public TimeCountUtil(Button button, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mButton = button;
}
@Override
public void onTick(long millisUntilFinished) {
// 按钮不可用
mButton.setEnabled(false);
String showText = millisUntilFinished /1000 +"秒";
mButton.setText(showText);
}
@Override
public void onFinish() {
// 按钮设置可用
mButton.setEnabled(true);
mButton.setText("获取验证码");
}
网友评论