android利用handler實現(xiàn)倒計時功能
本文實例為大家分享了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 {
/**
* 倒計時標記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 自定義SeekBar動態(tài)改變硬件音量大小實現(xiàn)和音量鍵的同步(推薦)
這篇文章主要介紹了 Android 自定義SeekBar動態(tài)改變硬件音量大小實現(xiàn)和音量鍵的同步效果,整段代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01
Android仿新浪微博oauth2.0授權(quán)界面實現(xiàn)代碼(2)
這篇文章主要為大家詳細介紹了Android仿新浪微博oauth2.0授權(quán)界面實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程使用自定義shape實現(xiàn)shadow陰影效果的方法
這篇文章主要介紹了Android編程使用自定義shape實現(xiàn)shadow陰影效果的方法,涉及Android中xml文件布局的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android OnCreate()中獲取控件高度與寬度兩種方法詳解
這篇文章主要介紹了Android OnCreate()中獲取控件高度與寬度兩種方法詳解的相關(guān)資料,這里提供了兩種方法,大家可以都看下,需要的朋友可以參考下2016-12-12
Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問題解決辦法
這篇文章主要介紹了ScrollView 下嵌套 ListView 或 GridView 會發(fā)列表現(xiàn)數(shù)據(jù)只能顯示一行。因為他們都是滾動結(jié)構(gòu),兩個滾動條放到一起就會引起沖突,這里提供解決辦法相關(guān)資料,需要的朋友可以參考下2017-07-07
Android多功能時鐘開發(fā)案例(基礎(chǔ)篇)
這篇文章主要為大家詳細介紹了Android多功能時鐘開發(fā)案例的基礎(chǔ)知識,為開發(fā)Android時鐘打下基礎(chǔ),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05

