0
点赞
收藏
分享

微信扫一扫

Android 获取验证码倒计时实现

1. 验证码输入框和获取验证码按钮布局

xml代码:

"match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >

<EditText
android:id="@+id/phonetext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:inputType="number"
android:hint="请输入短信验证码"
android:background="@null"/>

<Button
android:id="@+id/timebutton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textSize="16dp"
android:background="@drawable/tv_timemessage_bg"
android:text="获取"

效果如下:

Android 获取验证码倒计时实现_Android

2. 根据id设置Button点击事件触发倒计时

JAVA代码:

/**
* Created by fby on 2017/9/11.
*/
public class ChargepsdActivity extends Activity {

private Button timeButton;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chargepsd);

timeButton = (Button) findViewById(R.id.timebutton);
//new倒计时对象,总共的时间,每隔多少秒更新一次时间
final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);

//设置Button点击事件触发倒计时
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myCountDownTimer.start();
}
});

}

3. 倒计时函数

false);
timeButton.setText(l/1000+"秒");

}

//计时完毕的方法
@Override
public void onFinish() {
//重新给Button设置文字
timeButton.setText("重新获取");
//设置可点击
timeButton.setClickable(true);
}
}

}

4. 清除倒计时函数,解决验证码输入正确后停止计时

private void clearTimer() {
if (task != null) {
task.cancel();
task = null;
}
if

关注 【网罗开发】微信公众号,网罗天下方法,方便你我开发,更多Android技术干货等待领取,所有文档会持续更新,欢迎关注一起成长!

希望可以帮助大家

如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议

举报

相关推荐

0 条评论