private TimeCount time;
time = new TimeCount(60000, 1000);
class TimeCount extends CountDownTimer{
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
btnCode.setBackgroundResource(R.drawable.register_btn_code_huibg);
btnCode.setClickable(false);
btnCode.setText(millisUntilFinished/ 1000+"秒后重新发送");
}
@Override
public void onFinish() {
btnCode.setText("重新获取验证码");
btnCode.setClickable(true);
btnCode.setBackgroundResource(R.drawable.register_btn_code_bg);
}
}
time.start();
R.drawable.register_btn_code_huibg:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#B6B6D8" />
<corners android:radius="8dp"/>
</shape>
</item>
</layer-list>
R.drawable.register_btn_code_bg:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/heading_color" />
<corners android:radius="8dp"/>
</shape>
</item>
</layer-list>
xml文件:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:padding="8dp"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:src="@drawable/register_phone"
android:id="@+id/register_iv"
/>
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:text="@string/register_btn_code"
android:layout_centerVertical="true"
android:textSize="14sp"
android:textColor="@color/white"
android:background="@drawable/register_btn_code_bg"
style="?android:attr/borderlessButtonStyle"
android:id="@+id/register_code_btn"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/register_iv"
android:layout_toLeftOf="@+id/register_code_btn"
android:layout_centerVertical="true"
android:background="@null"
android:hint="@string/login_hint_name"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="16sp"
android:singleLine="true"
android:inputType="phone"
android:gravity="left"
android:id="@+id/register_et_name"
/>
</RelativeLayout>
效果图: