最近开始学习handler,觉得这个小demo对handler的理解挺有帮助的。
先看布局页面:

布局文件就不多说,差不多个意思就行。
需求分析
- 一个EditText输入验证码。
- 一个发送验证码的按钮Button。当点击这个按钮之后,该按钮就不能再被点击,Button里面的text变为一个30秒的倒数,30秒内无法重新发送验证码(也就是不能被点击)。在这30秒内如果输入验证码并点击登录按钮,将停止倒计时,并显示登录信息。如果30秒倒计时结束,登录按钮都没有被点击,那么发送验证码的按钮将恢复可点击状态,并且该按钮的文本变更为“重新发送”。
- 一个登录按钮Button。当按钮被点击,首先获取EditText里面的文本,如果内容为空,则吐司提示不能为空;如果内容不为空,则显示登录成功,并且停止验证码重新发送的倒计时。
- 一个TextView,用来显示登录成功字样(也可以单独写个页面跳转)。
实现思路
- onCreate方法

- 初始化组件

- 给button设置监听事件

- handler的信息处理

- 在onDestroy方法记得停止handler的looper的执行

完整代码 - 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/verify_code_et"
android:inputType="number"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="请输入验证码"/>
<Button
android:id="@+id/send_verify_code_btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="发送验证码"
android:layout_toRightOf="@+id/verify_code_et"/>
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
<TextView
android:id="@+id/test_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试"/>
</LinearLayout>
- `MainActivity.java
public class MainActivity extends AppCompatActivity {
private Handler mHandler;
private static final int MSG_SEND = 0;
public static final int MSG_LOGIN= MSG_SEND +1;
private Button mLoginBtn;
private TextView mTestTV;
private EditText mVerifyCodeET;
private Button mSendVerifyCodeBtn;
private int timeCount = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
HandlerThread handlerThread = new HandlerThread("myHandlerThread");
handlerThread.start();
mHandler = new MyMainHandler();
}
private void initListener() {
mSendVerifyCodeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mHandler.sendEmptyMessageDelayed(MSG_SEND,1000);
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String code = mVerifyCodeET.getText().toString().trim();
if(TextUtils.isEmpty(code)){
Toast.makeText(MainActivity.this,"驗證碼不能為空!",Toast.LENGTH_SHORT).show();
}else {
mHandler.sendEmptyMessage(MSG_LOGIN);
}
}
});
}
private void initView() {
mVerifyCodeET = findViewById(R.id.verify_code_et);
mSendVerifyCodeBtn = findViewById(R.id.send_verify_code_btn);
mLoginBtn = findViewById(R.id.login_btn);
mTestTV = findViewById(R.id.test_tv);
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.getLooper().quit();
}
class MyMainHandler extends Handler{
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what){
case MSG_SEND:
mSendVerifyCodeBtn.setText(timeCount-- + "s");
mSendVerifyCodeBtn.setEnabled(false);
mHandler.sendEmptyMessageDelayed(MSG_SEND,1000);)
if(timeCount==0){
mHandler.removeMessages(MSG_SEND);
mSendVerifyCodeBtn.setText("重新發送");
mSendVerifyCodeBtn.setEnabled(true);
timeCount=30;
}
break;
case MSG_LOGIN:
mHandler.removeMessages(MSG_SEND);
timeCount=30;
mTestTV.setText("歡迎您,登錄成功!");
break;
}
}
}
}
效果图

这篇博客介绍了如何在Android应用中使用Handler实现一个发送验证码按钮的30秒倒计时功能。在倒计时期间,按钮变为不可点击状态,若在30秒内完成登录则停止倒计时,否则倒计时结束后恢复按钮功能。通过布局文件和MainActivity.java代码展示了具体实现过程。

2371

被折叠的 条评论
为什么被折叠?



