在最近的项目开发中,由于多个模块都涉及到获取手机验证码,在获取验证码的过程中有个60s的倒计时,于是将这个倒计时的方法抽成一个通用的接口,方便各个模块调用。
运行效果
编码实现
1. 项目结构
2. 定义全局Context的获取类MyApplication
public class MyApplication extends Application {
private static MyApplication sInstance;
@Override
public void onCreate() {
sInstance = this;
}
public static MyApplication getInstance() {
return sInstance;
}
}
2. 定义倒计时类MyCountdown
public class MyCountdown extends CountDownTimer {
private Button mButton;
/**
* 构造函数
* @param button:按钮
* @param millisInFuture::倒计时总时间
* @param countDownInterval:倒计时时间间隔
*/
public MyCountdown(Button button, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
mButton = button;
}
@Override
public void onFinish() {
// 获取验证码按钮可点击
setButtonEnable();
}
@Override
public void onTick(long millisUntilFinished) {
// 获取验证码按钮不可点击,倒计时开始
setButtonUnEnable(millisUntilFinished);
}
@SuppressWarnings("deprecation")
public void setButtonEnable() {
mButton.setEnabled(true);
mButton.setText("重新获取");
mButton.setBackgroundDrawable(MyApplication.getInstance().
getResources().getDrawable(R.drawable.shape_button_red));
}
@SuppressWarnings("deprecation")
public void setButtonUnEnable(long millisUntilFinished) {
mButton.setEnabled(false);
mButton.setText("重新获取(" + millisUntilFinished / 1000 + ")秒");
mButton.setBackgroundDrawable(MyApplication.getInstance().
getResources().getDrawable(R.drawable.shape_button_gray));
}
}
3. 主布局文件activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_get_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_button_red"
android:text="获取验证码"
android:textColor="@color/light_gray" />
</RelativeLayout>
shape_button_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="6.0dip" />
<solid android:color="@color/red" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="6.0dip" />
<solid android:color="@color/gray" />
</shape>
4. MainActivity.java
public class MainActivity extends Activity {
private Button mGetCodeBtn;
private MyCountdown mCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGetCodeBtn = (Button) findViewById(R.id.btn_get_code);
mGetCodeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mCounter = new MyCountdown(mGetCodeBtn, 60 * 1000, 1000);
mCounter.start();
}
});
}
}