Button getVerCodeButton = (Button) findViewById(R.id.login_get_ver_code); private class VerCodeTimer extends CountDownTimer { private int seco...
Button getVerCodeButton = (Button) findViewById(R.id.login_get_ver_code);
private class VerCodeTimer extends CountDownTimer { private int seconds; private int interval; private int nowTime; //millisInFuture為你設置的此次倒計時的總時長,比如60秒就設置為60000 //countDownInterval為你設置的時間間隔,比如一般為1秒,根據需要自定義。 public VerCodeTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); seconds = (int) (millisInFuture / 1000); interval= (int) (countDownInterval/1000); nowTime=seconds; } //每過你規定的時間間隔做的操作 @Override public void onTick(long millisUntilFinished) { nowTime-=interval; getVerCodeButton.setText(nowTime + "秒後重新獲取"); } //倒計時結束時做的操作↓↓ @Override public void onFinish() { getVerCodeButton.setTextSize(10); getVerCodeButton.setText("重新獲取驗證碼"); getVerCodeButton.setClickable(true); getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_before_bg); } }
使用的時候:
getVerCodeButton.setTextSize(11); getVerCodeButton.setClickable(false); getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_ago_bg); mVerCodeTimer = new VerCodeTimer(10000, 1000); mVerCodeTimer.start();
login_edit_normal_bg.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:useLevel="false"> <!-- 背景填充顏色值 --> <solid android:color="#6c948b" /> <!-- radius值越大,越趨於圓形 --> <corners android:radius="10dip" /> <!-- 圓角圖像內部填充四周的大小 ,將會以此擠壓內部佈置的view --> <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> </shape>
login_edit_passed_bg.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:useLevel="false"> <!-- 背景填充顏色值 --> <solid android:color="#509989" /> <!-- radius值越大,越趨於圓形 --> <corners android:radius="10dip" /> <!-- 圓角圖像內部填充四周的大小 ,將會以此擠壓內部佈置的view --> <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> </shape>