Simple implementation of Android verification code countdown

In Android development, the verification code acquisition function is used in many APPs. After the verification code is clicked, the interface for obtaining the verification code will have time changes and restrictions, and the button for obtaining the verification code cannot be pressed. The operation to implement this interface is as follows:
First, we create a Button in the XML file, name it Captcha, and set various attributes, so I won’t go into details here.
Next, we write the Activity.java file. The countdown effect in the .java file is implemented using the CountDownTime class.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    
    private TimeCount time;
    private Button bt;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        time=new TimeCount(60000,1000);
        bt=findViewById(R.id.Captcha);
        bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
    
    
        switch(view.getId()){
    
    
            case R.id.Captcha:
                time.start();
                break;
        }
    }

    class TimeCount extends CountDownTimer{
    
    
        public TimeCount(long m,long n){
    
    
            super(m,n);
        }

        @Override
        public void onTick(long l) {
    
    
            bt.setBackgroundColor(Color.parseColor("#B6B6D8"));
            bt.setClickable(false);
            bt.setText("("+l/1000+")秒后可重新发送");
        }

        @Override
        public void onFinish() {
    
    
            bt.setText("重新获取验证码");
            bt.setClickable(true);
            bt.setBackgroundColor(Color.parseColor("#4EB84A"));
        }
    }
}

Welcome to the big guys to comment and discuss·······

Guess you like

Origin blog.csdn.net/weixin_45457983/article/details/104557144