色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Android實現計時器功能

瀏覽:4日期:2022-09-19 13:16:14

本文實例為大家分享了Android實現計時器功能的具體代碼,供大家參考,具體內容如下

計時器工具類

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; //回調 private Timer timer; // 定時器 private static final int WHAT_REFREH = 0;//刷新 /** * 創建對象則開始計時 * * @param chjTimerInter 接口回調 */ public ChjTimer(ChjTimerInter chjTimerInter) {this.chjTimerInter = chjTimerInter; } /** * 創建對象開始計時 * @param interval 間隔時間通知(使用第一個方法,默認1秒鐘刷新一次) * @param chjTimerInter 接口回調 */ 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() {@Overridepublic 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 { /** * 間隔時間內回調 */void second(int time); /** * 完成回調 */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'> <TextViewandroid: 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' /> <TextViewandroid: android:text='0'android:layout_width='wrap_content'android:layout_height='wrap_content' /> <Buttonandroid: android:text='開始'android:layout_width='wrap_content'android:layout_height='wrap_content' /> <Buttonandroid: android:text='終止'android:layout_width='wrap_content'android:layout_height='wrap_content' /> </LinearLayout>

演示效果

Android實現計時器功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 国产在线高清不卡免费播放 | 草草影院视频 | 中国美女牲交一级毛片 | 日韩欧美成人乱码一在线 | 国产精品一一在线观看 | 亚洲欧美专区精品久久 | 精品成人久久 | 福利社色 | 久久精品国产半推半就 | 久草新| 2021国内自拍 | 国产精品免费看久久久久 | 国产黄色网 | 久草视频中文在线 | 国产亚洲亚洲精品777 | 国产在线步兵一区二区三区 | 玖玖爱zh综合伊人久久 | 亚洲国产精久久久久久久 | 九九精品视频一区在线 | 在线毛片一区二区不卡视频 | 狠狠综合久久久综合 | 亚洲高清成人欧美动作片 | 国产91久久精品一区二区 | 成年性午夜免费视频网站不卡 | 曰本美女高清在线观看免费 | 二区国产 | 综合爱爱网| 美国三级大片 | 视频二区好吊色永久视频 | 国产精品免费视频一区二区三区 | 亚洲国产成人在人网站天堂 | 久久久久久久久久久久久久久久久 | yy6080久久亚洲精品 | 在线免费一区二区 | 一级爱做片免费观看久久 | 美女三级黄 | 国产成人精品视频在放 | 国产精品精品 | 国产一级特黄特色aa毛片 | 亚洲精品国产三级在线观看 | 国产激情一区二区三区 |