Android 获取验证码倒计时实现

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

xml代码:

        <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.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(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 onFinish() {
            //重新给Button设置文字
            timeButton.setText("重新获取");
            //设置可点击
            timeButton.setClickable(true);
        }
    }

}

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

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

希望可以帮助大家
如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,262评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 回想一年前,现在应该还坐在教室里,听着班干的训斥声,望一眼前排那个有着世界上最好看侧颜女生的马尾,默不作声的在纸上...
    艾越阅读 1,423评论 0 0
  • 正视自己的状态,与对方沟通,达成共识。
    小胡串阅读 1,501评论 0 0