android登陆获取验证码倒计时功能的实现(CountDownTimer)
来源:互联网 发布:v7000数据迁移图文教程 编辑:程序博客网 时间:2024/05/17 20:08
最近公司做一个手机商城APP,注册用户时需要用,用户手机号注册账号,现在基本大部分APP注册都是这样了。遇到了就记下来,方便以后再用,毕竟写过的代码久了不写还是会忘记的。
看了一下官方文档,这个类及其简单,只有四个方法,上面都涉及到了onTick,onFinsh、cancel和start。其中前面两个是抽象方法,所以要重写一下。
下面是一个小例子:
public class MainActivity extends Activity implements OnClickListener {
private EditText ed_phone;// 手机号
private Button ed_button;// 验证码按钮
private String phone;
private Timecount timecount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_phone = (EditText) findViewById(R.id.ed_phone);
ed_button = (Button) findViewById(R.id.ed_button);
ed_button.setOnClickListener(this);
timecount = new Timecount(60000, 1000);// 构造CountDownTimer对象
}
/**
* 定义倒计时内部类
*/
class Timecount extends CountDownTimer {
public Timecount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
}
@Override
public void onTick(long millisUntilFinished) {
ed_button.setClickable(false);
ed_button.setText(millisUntilFinished / 1000 + "s");
}
@Override
public void onFinish() {
ed_button.setText("重新发送");
ed_button.setClickable(true);
}
}
// 验证手机号是否为空
private void info() {
phone = ed_phone.getText().toString();
if (TextUtils.isEmpty(phone)) {
Toast.makeText(MainActivity.this, "请输入手机号码", Toast.LENGTH_SHORT)
.show();
return;
}
if (!IS_PHONE.matcher(phone).matches()) {
Toast.makeText(MainActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT)
.show();
return;
}
timecount.start();// 开始倒计时
}
//个人习惯这么写
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ed_button:
info();
break;
default:
break;
}
}
- android登陆获取验证码倒计时功能的实现(CountDownTimer)
- Android获取验证码倒计时功能(CountDownTimer实现)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- Android 倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- Android倒计时功能的实现(CountDownTimer)
- Android 倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- 转个苹果公司联系邮箱大全
- Android数据库安全解决方案,使用SQLCipher进行加解密
- 全功能DB数据库管理工具,超赞,完全支持所有主流数据库
- OCR备份后,我们看看里面的内容
- openfire源码研究笔记:对设计模式及原则的学习
- android登陆获取验证码倒计时功能的实现(CountDownTimer)
- 2016年 IT 趋势大预测!
- The content of the adapter has changed but ListView did not receive a notification异常处理
- 手写的Android命名规范
- Jmeter:图形界面压力测试工具
- Linux常用操作-压缩与解压缩
- 《从零开始学Swift》学习笔记(Day 59)——代码排版
- 编程习惯-函数无参数用void填充
- spring原理及工作方式