本文实例为大家分享了Android获取验证码倒计时的具体代码,供大家参考,具体内容如下
1. 验证码输入框和获取验证码按钮布局
代码:
<LinearLayout
android:layout_width="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="获取"
/>
</LinearLayout>
效果如下:

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.set Listener(new View. Listener() {
@Override
public void (View view) {
myCountDownTimer.start();
}
});
}
3. 倒计时函数
//倒计时函数
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//计时过程
@Override
public void onTick(long l) {
//防止计时过程中重复点击
timeButton.setClickable(false);
timeButton.setText(l/1000+"秒");
}
//计时完毕的方法
@Override
public void () {
//重新给Button设置文字
timeButton.setText("重新获取");
//设置可点击
timeButton.setClickable(true);
}
}
}
4. 清除倒计时函数,解决验证码输入正确后停止计时
private void clearTimer() {
if (task != null) {
task.cancel();
task = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
SpringBoot入门之集成JSP的示例代码
下一篇 :
微信支付的B面
-
微信指纹聊天特效,好友看了一直擦屏幕!
2026-05-15栏目: 教程
-
微信通知的壁纸锁屏,有意思!
2026-05-15栏目: 教程
-
阿联酋人工智能大学在阿布扎比成立
2026-05-15栏目: 教程
-
未来将消失的8种职业,第一种最扎心,人工智能成最大“威胁”
2026-05-15栏目: 教程
-
2019年度8款的机器人吸尘器,你值得拥有!
2026-05-15栏目: 教程
