Android實現(xiàn)計時器功能
更新時間:2021年04月19日 09:38:04 作者:地心鐵頭娃
這篇文章主要為大家詳細介紹了Android實現(xiàn)計時器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(xiàn)計時器功能的具體代碼,供大家參考,具體內(nèi)容如下
計時器工具類
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import java.util.Timer;
import java.util.TimerTask;
/**
* 用于計時,在主線程中使用此方法
*/
public class ChjTimer {
private int time;//設置倒計時 X 秒
private int interval = 1000;//設置間隔時間
private ChjTimerInter chjTimerInter; //回調(diào)
private Timer timer; // 定時器
private static final int WHAT_REFREH = 0;//刷新
/**
* 創(chuàng)建對象則開始計時
*
* @param chjTimerInter 接口回調(diào)
*/
public ChjTimer(ChjTimerInter chjTimerInter) {
this.chjTimerInter = chjTimerInter;
}
/**
* 創(chuàng)建對象開始計時
* @param interval 間隔時間通知(使用第一個方法,默認1秒鐘刷新一次)
* @param chjTimerInter 接口回調(diào)
*/
public ChjTimer(int interval, ChjTimerInter chjTimerInter) {
this.chjTimerInter = chjTimerInter;
this.interval = interval;
}
/**
* 開始計時
*/
public void start(int time) {
this.time = time;
if (timer == null){
timer = new Timer();
} else {
stop();
return;
}
timer.schedule(new TimerTask() {
@Override
public void run() {
timesss.sendMessage(new Message());
}
}, interval);
}
/**
* 終止計時
*/
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timesss != null) timesss.removeMessages(WHAT_REFREH);
if (chjTimerInter != null)chjTimerInter.stop(time);
}
@SuppressLint("HandlerLeak")
private Handler timesss = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what != WHAT_REFREH) return;
time -= 1;
if (chjTimerInter != null) chjTimerInter.second(time);
if (time == 0) {
if (timer == null) return;
timer.cancel();
timer = null;
if (chjTimerInter != null) chjTimerInter.expire();
} else if (time > 0) {
timer.schedule(new TimerTask() {
@Override
public void run() {
timesss.sendMessage(new Message());
}
}, interval);
}
}
};
/**
* 接口
*/
public interface ChjTimerInter {
/**
* 間隔時間內(nèi)回調(diào)
*/
void second(int time);
/**
* 完成回調(diào)
*/
void expire();
/**
* 終止計時
*/
void stop(int time);
}
}
使用演示
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener, ChjTimer.ChjTimerInter {
private TextView tiems,timnew;
private ChjTimer chjTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tiems = findViewById(R.id.time);
timnew = findViewById(R.id.timnew);
findViewById(R.id.but).setOnClickListener(this);
findViewById(R.id.buts).setOnClickListener(this);
chjTimer = new ChjTimer(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.but:
tiems.setText("10");
timnew.setText("正在計時");
chjTimer.start(10);
break;
case R.id.buts:
chjTimer.stop();
break;
}
}
@Override
public void second(int time) {
tiems.setText(time + "");
}
@Override
public void expire() {
timnew.setText("計時完成");
}
@Override
public void stop(int time) {
timnew.setText("計時終止" + time);
}
}
頁面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/timnew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="計時完成"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/time"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/but"
android:text="開始"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buts"
android:text="終止"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
演示效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)之組件GridView簡單使用方法示例
這篇文章主要介紹了Android開發(fā)之組件GridView簡單使用方法,涉及Android GridView組件圖片瀏覽及保存圖片等相關操作技巧,需要的朋友可以參考下2019-03-03
flutter Container容器實現(xiàn)圓角邊框
這篇文章主要為大家詳細介紹了flutter Container容器實現(xiàn)圓角邊框,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
實現(xiàn)輪轉(zhuǎn)廣告帶底部指示的自定義ViewPager控件
在項目中經(jīng)常需要使用輪轉(zhuǎn)廣告的效果,在android-v4版本中提供的ViewPager是一個很好的工具,而一般我們使用Viewpager的時候,都會選擇在底部有一排指示物指示當前顯示的是哪一個page,下面我們就做這個功能的實現(xiàn)2013-11-11
Android 系統(tǒng)語言切換監(jiān)聽和設置實例代碼
本篇文章主要介紹了Android 系統(tǒng)語言切換監(jiān)聽和設置實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android實現(xiàn)仿excel數(shù)據(jù)表格效果
這篇文章主要介紹了Android實現(xiàn)仿excel數(shù)據(jù)表格效果的實現(xiàn)代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下
2016-10-10 
