android利用handler實現(xiàn)倒計時功能
更新時間:2020年11月24日 11:55:13 作者:codeTcy
這篇文章主要為大家詳細(xì)介紹了android利用handler實現(xiàn)倒計時功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android利用handler實現(xiàn)倒計時的具體代碼,供大家參考,具體內(nèi)容如下
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 {
/**
* 倒計時標(biāo)記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發(fā)出去
Message message = Message.obtain();
message.what = COUNT_DOWN_CODE;
message.arg1 = value;
sendMessageDelayed(message, DELAY_MILLIS);
}
break;
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android利用Camera實現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
這篇文章主要介紹了Android利用Camera實現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果,需要的朋友可以參考下2015-12-12
Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時間功能示例
這篇文章主要介紹了Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時間功能,結(jié)合實例形式分析了Android布局、事件響應(yīng)、監(jiān)聽以及時間獲取相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
解析:ClickOnce通過URL傳遞參數(shù) XXX.application?a=1
本篇文章是對ClickOnce通過URL傳遞參數(shù)進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android垂直切換的圓角Banner與垂直指示器相關(guān)介紹與應(yīng)用詳解
這篇文章主要介紹了Android垂直切換的圓角Banner與垂直指示器相關(guān)介紹與應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
Android實現(xiàn)TextView字符串關(guān)鍵字變色的方法
這篇文章顯示給大家介紹了字符串中關(guān)鍵字變色的實現(xiàn)方法,而后又拓展介紹了在Android中如何實現(xiàn)搜索關(guān)鍵字變色,相信對各位Android開發(fā)者們具有一定的參考借鑒價值,感興趣的朋友們下面來一起看看吧。2016-10-10

