Android之发送短信后按钮(60秒)变灰色每隔一秒递减显示

来源:互联网 发布:曲阜网络问政 编辑:程序博客网 时间:2024/05/16 01:34

无论是做PC端还是android端,每次注册都有发送短信之后,60秒每隔一秒递减显示,如下图



这个地方需要注意的是按钮变灰色之后不能再点击,然后就是android更新UI,需要用handle,或者其它post方式,关键代码如下


/**     * 显示时间在梯减的文本框     */    public void showTime() {        new Thread(new Runnable() {            @Override            public void run() {                while (result) {                    time--;                    try {                        Thread.sleep(1000);//                                     tvShowTime.setText(time + "s后从新获取");                        btGetVerificationCode.post(new Runnable() {                            @Override                            public void run() {                                btGetVerificationCode.setText(time + "s后重新获取");                                btGetVerificationCode.setClickable(false);                                btGetVerificationCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.text_layout_bg_codenotedit));                            }                        });                        if (time <= 1) {                            count=0;                            result = false;                            btGetVerificationCode.post(new Runnable() {                                @Override                                public void run() {                                    btGetVerificationCode.setText("获取验证码");                                    btGetVerificationCode.setClickable(true);                                    btGetVerificationCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_btn_red));                                }                            });                        }                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                result = true;                time = 60;            }        }).start();    }


初始化

    private Button btGetVerificationCode;    private Button btRegister;    private boolean result = true;    private int time = 60;    private int count = 0;



1 0
原创粉丝点击