Android implementation of verification code login

Android implementation of verification code login

Result display
Insert picture description here

1. Guide package

1.1 Import in the gradle of the project

maven {
    
     url "https://www.jitpack.io" }

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-CIxN1n6A-1615717233861)(C:\Users\MoFish\AppData\Roaming\Typora\typora-user-images\ image-20210314181656741.png)]

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-HiPmsIyU-1615717233868)(C:\Users\MoFish\AppData\Roaming\Typora\typora-user-images\ image-20210314181630575.png)]

1.2 Import in the gradle dependencies of the model

//XUI项目
implementation 'com.github.xuexiangjys:XUI:1.1.6'

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-Dm14pzEe-1615717233873)(C:\Users\MoFish\AppData\Roaming\Typora\typora-user-images\ image-20210314181825191.png)]

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-LlVlVIHA-1615717233881)(C:\Users\MoFish\AppData\Roaming\Typora\typora-user-images\ image-20210314181840923.png)]

1.3 Click sync now in the upper right corner

2. Create a new xml file

phone_code.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="50dp"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text="请输入验证码"
        />
    <TextView
        android:id="@+id/phone_number_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:textColor="#000000"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="5dp"
        />

    <com.xuexiang.xui.widget.edittext.verify.VerifyCodeEditText
        android:id="@+id/phone_code_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="26dp"
        android:layout_marginRight="10dp"
        app:vcet_is_pwd="false"
        app:vcet_number="6"
        app:vcet_pwd_radius="10dp"
        app:vcet_text_color="#000000"
        app:vcet_width="50dp" />

    <TextView
        android:id="@+id/re_get_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="30dp"
        android:textColor="#60000000"
        android:textSize="20dp"
        />

    <TextView
        android:id="@+id/get_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="30dp"
        android:textColor="#60000000"
        android:textSize="15dp"
        />

</LinearLayout>

  1. Modify Activity

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;


import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    
    TextView phoneNumberStr;
    TextView codeCountDown;
    TextView reGetCode;
    private int recLen = 10;

    Timer timer = new Timer();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phone_code);
        init();//初始化组件
        String phone = new String("15968373790");

        if (phone.length() < 11)
            phoneNumberStr.setText("验证码已发送至"+phone);
        else
            phoneNumberStr.setText("验证码已发送至"+phone.substring(0,3)+"****"+phone.substring(7));
        timer.schedule(task, 1000, 1000);       // 启动一个1000毫秒(1秒)的定时任务

    }

    TimerTask task = new TimerTask() {
    
    
        @Override
        public void run() {
    
    

            runOnUiThread(new Runnable() {
    
    
                @Override
                public void run() {
    
    
                    codeCountDown.setVisibility(View.VISIBLE);
                    recLen--;
                    codeCountDown.setText(recLen+"秒后重新获取验证码");//动态调整秒数下降
                    if(recLen <= 0){
    
    
                        timer.cancel();
                        codeCountDown.setVisibility(View.GONE);
                        reGetCode.setText("重新获得验证码");//倒计时结束,修改为重新获得验证码

                        reGetCode.setVisibility(View.VISIBLE);//修改控件的可见性
                        reGetCode.setOnClickListener(new View.OnClickListener() {
    
    
                            @Override
                            public void onClick(View v) {
    
    
                                reGetCode.setVisibility(View.GONE);
                                recLen = 10;
                                codeCountDown.setVisibility(View.VISIBLE);
                                codeCountDown.setText(recLen+"秒后重新获取验证码");
                                timer = new Timer();
                                //task一般情况下使用过一次后无法再使用,但可以借助反射使得task重新工作,修改state属性即可,state为1时表示已经使用过无法再次使用,为0表示可以使用
                                Field field;
                                try {
    
    
                                    field = TimerTask.class.getDeclaredField("state");
                                    field.setAccessible(true);
                                    field.set(task, 0);
                                } catch (NoSuchFieldException e) {
    
    
                                    e.printStackTrace();
                                } catch (Exception e) {
    
    
                                    e.printStackTrace();
                                }
                                timer.schedule(task, 1000, 1000);
                            }
                        });
                    }
                }
            });
        }
    };


    private void init() {
    
    

        phoneNumberStr = findViewById(R.id.phone_number_str);
        codeCountDown = findViewById(R.id.re_get_code);
        reGetCode = findViewById(R.id.re_get_code);
        reGetCode.setOnClickListener(this);
        reGetCode.setVisibility(View.GONE);
    }


    @Override
    public void onClick(View v) {
    
    
        Intent intent;//设置单击事件使得倒计时可以继续
        switch (v.getId()){
    
    
            case R.id.get_code:
                reGetCode.setVisibility(View.GONE);
                timer.schedule(task, 1000, 1000);       // timeTask
                break;
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_50665031/article/details/114796718