本文为大家分享了CountDownTimer模拟短信验证倒计时的具体代码,供大家参考,具体内容如下
<强>内容:强>介绍倒计时CountDownTimer的基本使用方法。模拟短信验证
<强>步骤:强>
1,继承CountDownTimer,重写onTick (), onFinish ()
2代码中新的出CountDownTimer子类,传好参数,调用开始()执行
代码如下:
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " 工具:上下文=癱om.example.leixiansheng.countdowntimer.MainActivity”比; & lt; TextView android: id=癅 + id/tv_getMsg” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_centerInParent=" true " android:文本="获取短信验证码” android:背景=" @color/colorPrimaryDark” android: textSize=" 16 sp " android:输入textColor=" # ffffffff "/比; & lt;/RelativeLayout>
TimerCount
包com.example.leixiansheng.countdowntimer; 进口android.os.CountDownTimer; 进口android.widget.TextView;/* * *由Leixiansheng> 包com.example.leixiansheng.countdowntimer; 进口android.support.v7.app.AppCompatActivity; 进口android.os.Bundle; 进口android.view.View; 进口android.widget.TextView; 公开课MainActivity延伸AppCompatActivity { @Override 保护无效onCreate(包savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); 最后一个TextView TextView=(TextView) findViewById (R.id.tv_getMsg); textView。setOnClickListener(新View.OnClickListener () { @Override 公共空间onClick(查看v) {/* * * millisInFuture:要计数的总时长 * countDownInterval:每隔多少秒响应 */TimerCount TimerCount=new TimerCount (5000、1000、textView); timerCount.start (); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持只