这个功能很常用,几乎每个app都要注册需要用的,废话不多说直接上代码
xml 里面的代码:
<TextView android:id="@+id/regis_btn_code" android:layout_width="wrap_content" android:layout_height="@dimen/distance_twenty_seven" android:background="@drawable/bg_solid_gray_ten" android:layout_marginRight="@dimen/distance_twenty_four" android:layout_marginTop="@dimen/distance_two" android:textColor="@color/white" android:text="@string/obtain_code" android:gravity="center" android:paddingLeft="@dimen/distance_ten" android:paddingRight="@dimen/distance_ten" android:paddingBottom="@dimen/distance_five" android:paddingTop="@dimen/distance_five" android:textSize="@dimen/txt_twelve" android:enabled="false" android:layout_centerVertical="true"/>
点击事件实现的方法:
TimeCount timeCount = new TimeCount(frequency *time,1000,mSendCode,"重新发送"); timeCount.start();
time为时间间隔,frequency为次数,我这里间隔是60秒。
TimeCount里面的代码:
public class TimeCount extends CountDownTimer { private TextView mSendCode; private String toast; public TimeCount(long millisInFuture, long countDownInterval,TextView mSendCode,String toast) { super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔 this.mSendCode = mSendCode; this.toast = toast; } @Override public void onFinish() {//计时完毕时触发 mSendCode.setText(toast); mSendCode.setEnabled(true); mSendCode.setBackgroundResource(R.drawable.bg_solid_blue_ten); } @Override public void onTick(long millisUntilFinished){//计时过程显示 mSendCode.setEnabled(false); mSendCode.setBackgroundResource(R.drawable.bg_solid_gray_ten); mSendCode.setText(millisUntilFinished /1000+"秒后重试"); } }