午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android實(shí)現(xiàn)計(jì)時(shí)器功能

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

本文實(shí)例為大家分享了Android實(shí)現(xiàn)計(jì)時(shí)器功能的具體代碼,供大家參考,具體內(nèi)容如下

計(jì)時(shí)器工具類(lè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; /** * 用于計(jì)時(shí),在主線程中使用此方法 */public class ChjTimer { private int time;//設(shè)置倒計(jì)時(shí) X 秒 private int interval = 1000;//設(shè)置間隔時(shí)間 private ChjTimerInter chjTimerInter; //回調(diào) private Timer timer; // 定時(shí)器 private static final int WHAT_REFREH = 0;//刷新 /** * 創(chuàng)建對(duì)象則開(kāi)始計(jì)時(shí) * * @param chjTimerInter 接口回調(diào) */ public ChjTimer(ChjTimerInter chjTimerInter) {this.chjTimerInter = chjTimerInter; } /** * 創(chuàng)建對(duì)象開(kāi)始計(jì)時(shí) * @param interval 間隔時(shí)間通知(使用第一個(gè)方法,默認(rèn)1秒鐘刷新一次) * @param chjTimerInter 接口回調(diào) */ public ChjTimer(int interval, ChjTimerInter chjTimerInter) {this.chjTimerInter = chjTimerInter;this.interval = interval; } /** * 開(kāi)始計(jì)時(shí) */ 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); } /** * 終止計(jì)時(shí) */ 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 { /** * 間隔時(shí)間內(nèi)回調(diào) */void second(int time); /** * 完成回調(diào) */void expire(); /** * 終止計(jì)時(shí) */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('正在計(jì)時(shí)');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('計(jì)時(shí)完成'); } @Override public void stop(int time) {timnew.setText('計(jì)時(shí)終止' + time); }}

頁(yè)面布局

<?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='計(jì)時(shí)完成'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='開(kāi)始'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實(shí)現(xiàn)計(jì)時(shí)器功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 成年人网站免费看 | 毛片手机在线观看 | 精品无人国产偷自产在线 | 国产日韩在线免费观看 | 免费成人深夜在线观看 | 天天干天天狠 | 亚洲欧美另类日韩 | 极品美女一区二区三区 | 性爱视频在线免费 | 日韩一级av毛片 | 朝桐光av一区二区三区 | www.五月天婷婷 | 国内精品一区二区 | 天天躁日日躁狠狠躁喷水 | 日本韩国欧美中文字幕 | 欧美精品一级 | 麻豆国产91在线播放 | 成人在线观看网址 | 青青草自拍偷拍 | 怡红院成人在线 | 成人av免费观看 | 欧美成人激情视频 | 国产男女在线 | 超碰.com | 日韩欧美大片在线观看 | 国产免费aa | 久久久久久影视 | 亚洲免费一级片 | 国产午夜免费视频 | 91精品婷婷国产综合久久蝌蚪 | 人人干av| 日本二区三区视频 | 亚洲国产成人在线视频 | 久久久久中文字幕亚洲精品 | 男女做事网站 | 国产网址在线 | 蜜桃视频久久 | 国产四虎 | 成年在线观看 | 久久91精品国产 | 欧美大喷水吹潮合集在线观看 |