Android實現(xiàn)驗證碼登錄
本文實例為大家分享了Android實現(xiàn)驗證碼登錄的具體代碼,供大家參考,具體內(nèi)容如下
結(jié)果展示

1.導包
1.1在項目的gradle中導入
maven { url "https://www.jitpack.io" }


1.2在model的gradle的dependencies導入
//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'


1.3點擊右上角的sync now
2.新建xml文件
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>
3.修改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("驗證碼已發(fā)送至"+phone);
else
phoneNumberStr.setText("驗證碼已發(fā)送至"+phone.substring(0,3)+"****"+phone.substring(7));
timer.schedule(task, 1000, 1000); // 啟動一個1000毫秒(1秒)的定時任務(wù)
}
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
codeCountDown.setVisibility(View.VISIBLE);
recLen--;
codeCountDown.setText(recLen+"秒后重新獲取驗證碼");//動態(tài)調(diào)整秒數(shù)下降
if(recLen <= 0){
timer.cancel();
codeCountDown.setVisibility(View.GONE);
reGetCode.setText("重新獲得驗證碼");//倒計時結(jié)束,修改為重新獲得驗證碼
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時表示已經(jīng)使用過無法再次使用,為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;//設(shè)置單擊事件使得倒計時可以繼續(xù)
switch (v.getId()){
case R.id.get_code:
reGetCode.setVisibility(View.GONE);
timer.schedule(task, 1000, 1000); // timeTask
break;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android使用OKhttp3實現(xiàn)登錄注冊功能+springboot搭建后端的詳細過程
- android實現(xiàn)簡易登錄注冊界面及邏輯設(shè)計
- android 仿微信demo——登錄功能實現(xiàn)(服務(wù)端)
- android 仿微信demo——登錄功能實現(xiàn)(移動端)
- Android Studio實現(xiàn)QQ的注冊登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實現(xiàn)登錄注冊
- Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)
- Android Studio實現(xiàn)登錄功能案例講解
相關(guān)文章
學習使用Material Design控件(二)使用DrawerLayout實現(xiàn)側(cè)滑菜單欄效果
這篇文章主要為大家介紹了學習使用Material Design控件的詳細教程,使用DrawerLayout和NavigationView實現(xiàn)側(cè)滑菜單欄效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
詳解Android開啟OTG功能/USB?Host?API功能
這篇文章主要介紹了Android開啟OTG功能/USB?Host?API功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法,涉及Android開發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
在Android中使用WebSocket實現(xiàn)消息通信的方法詳解
這篇文章主要介紹了在Android中使用WebSocket實現(xiàn)消息通信的方法詳解,消息推送功能可以說移動APP不可缺少的功能之一,使用WebSocket實現(xiàn)消息推送功能。感興趣的可以了解一下2020-07-07
android通過Location API顯示地址信息的實現(xiàn)方法
這篇文章主要介紹了android通過Location API顯示地址信息的方法,涉及Android操作Geocoder類獲取地址信息的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
Android編程實現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
這篇文章主要介紹了Android編程實現(xiàn)popupwindow彈出后屏幕背景變成半透明效果,涉及Android設(shè)置getWindows透明度的方法,需要的朋友可以參考下2016-01-01

