android利用handler实现倒计时功能
作者:codeTcy 时间:2021-10-19 07:36:59
本文实例为大家分享了android利用handler实现倒计时的具体代码,供大家参考,具体内容如下
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
java
package com.tcy.handlertest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import java.lang.ref.WeakReference;
public class MainActivity extends AppCompatActivity {
/**
* 倒计时标记handler code
*/
public static final int COUNT_DOWN_CODE = 10001;
/**
* 倒计时最大值
*/
public static final int MAX_COUNT = 10;
/**
* 倒计时间隔
*/
public static final int DELAY_MILLIS = 1000;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
CountdownTimeHandler handler = new CountdownTimeHandler(this);
Message message = Message.obtain();
message.what = COUNT_DOWN_CODE;
message.arg1 = MAX_COUNT;
handler.sendMessageDelayed(message, DELAY_MILLIS);
}
public static class CountdownTimeHandler extends Handler {
//弱引用加在上下文上面
final WeakReference<MainActivity> weakReference;
//这个方法要改一下,这样就能直接传进来上下文
public CountdownTimeHandler(MainActivity activity) {
this.weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//得到上下文
MainActivity activity = weakReference.get();
switch (msg.what) {
case COUNT_DOWN_CODE:
int value = msg.arg1;
activity.textView.setText(String.valueOf(value--));
if (value >= 0) {
//再把value发出去
Message message = Message.obtain();
message.what = COUNT_DOWN_CODE;
message.arg1 = value;
sendMessageDelayed(message, DELAY_MILLIS);
}
break;
}
}
}
}
来源:https://blog.csdn.net/melocarter/article/details/109991275
标签:android,倒计时
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入理解C#窗体关闭事件
2023-06-01 14:38:56
![](https://img.aspxhome.com/file/2023/7/122637_0s.jpg)
Mybatis基于xml配置实现单表的增删改查功能
2021-09-29 11:21:48
java Apache poi 对word doc文件进行读写操作
2023-09-23 02:13:15
![](https://img.aspxhome.com/file/2023/7/62987_0s.png)
java执行Linux命令的方法
2023-01-23 21:36:10
Mybatis插件之自动生成不使用默认的驼峰式操作
2023-11-19 01:20:03
python 转换 Javascript %u 字符串为python unicode的代码
2022-07-28 03:15:14
C#判断字符串中内容是否为纯数字的详细教程
2022-12-05 07:44:38
浅谈java中守护线程与用户线程
2023-11-26 20:46:41
![](https://img.aspxhome.com/file/2023/3/60173_0s.png)
Java中静态代码块、构造代码块、构造函数和普通代码块的区别
2023-11-25 10:09:06
![](https://img.aspxhome.com/file/2023/8/60158_0s.png)
C#实现窗体淡入淡出效果的方法总结
2021-05-28 08:23:37
Mybatis防止sql注入原理分析
2023-08-09 22:54:44
Android Studio 3.1.3升级至3.6.1后旧项目的兼容操作方法
2022-03-27 10:15:41
![](https://img.aspxhome.com/file/2023/6/91646_0s.png)
基于IDEA中格式化代码的快捷键分享
2022-08-15 14:58:03
Android编程实现任务管理器的方法
2022-11-30 00:32:15
![](https://img.aspxhome.com/file/2023/5/122105_0s.png)
SpringBoot用配置影响Bean加载@ConditionalOnProperty
2022-11-28 22:16:07
![](https://img.aspxhome.com/file/2023/2/75482_0s.jpg)
JAVA中常见异常类
2021-11-09 09:47:20
Android实现流动的渐变色边框效果
2023-06-28 08:24:13
![](https://img.aspxhome.com/file/2023/9/117129_0s.png)
Android RxJava创建操作符Interval
2023-08-14 01:26:24
网易Java程序员两轮面试 请问你能答对几个?
2023-11-29 10:32:08
详解Spark Sql在UDF中如何引用外部数据
2021-08-17 14:51:17
![](https://img.aspxhome.com/file/2023/4/64124_0s.jpg)