android實(shí)現(xiàn)倒計(jì)時(shí)功能(開始、暫停、0秒結(jié)束)
本文實(shí)例為大家分享了android實(shí)現(xiàn)倒計(jì)時(shí)功能的具體代碼,供大家參考,具體內(nèi)容如下
【思路】:通過 timer 執(zhí)行周期延時(shí)的任務(wù),handler 中將計(jì)時(shí)信息更新,并在計(jì)時(shí)結(jié)束時(shí)結(jié)束 timer 的周期任務(wù)。
- 在布局文件中添加一個(gè)TextView和Button控件,并在onCreate方法中獲得到TextView和Button的id;
xml布局代碼:
<Button android:id="@+id/button_start_timer" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:gravity="center" android:text="開始" android:textSize="12sp" /> <TextView android:id="@+id/textViewTime24" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="2" android:gravity="center" android:text="24" android:textColor="#33ff00" android:textSize="60sp" />
java代碼
package com.example.wlf.gamerecorder.gameon; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.os.Handler; import com.example.wlf.gamerecorder.R; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class SimpleGameonActivity extends AppCompatActivity { private final static int COUNT = 1; private final static int TOTAL_TIME_24 = 24; private TextView textViewTime24; Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_gameon); textViewTime24=(TextView)findViewById(R.id.textViewTime24);//24秒倒計(jì)時(shí) final Button button_start_timer = (Button)findViewById(R.id.button_start_timer); button_start_timer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str = button_start_timer.getText().toString();//獲取按鈕字符串 if(str.equals("開始")){ //切換按鈕文字 button_start_timer.setText("暫停"); initView(); } else{ button_start_timer.setText("開始"); timer.cancel();//終止線程 } } }); } public void initView(){ //countDown = (TextView) findViewById(R.id.textViewTime24); timer = new Timer(); /** * 每一秒發(fā)送一次消息給handler更新UI * schedule(TimerTask task, long delay, long period) */ timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(COUNT); } }, 0, 1000); } private Handler handler = new Handler(){ int num = TOTAL_TIME_24; public void handleMessage(android.os.Message msg) { switch (msg.what) { case COUNT: textViewTime24.setText(String.valueOf(num)); if(num == 0) timer.cancel();//0秒結(jié)束 num--; break; default: break; } }; }; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android計(jì)時(shí)與倒計(jì)時(shí)實(shí)現(xiàn)限時(shí)搶購的5種方法
- Android限時(shí)搶購倒計(jì)時(shí)實(shí)現(xiàn)代碼
- android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
- android利用handler實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- Android實(shí)現(xiàn)倒計(jì)時(shí)效果
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯亂問題
- Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
- Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android 簡單實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例
相關(guān)文章
Android Studio 3.0上分析內(nèi)存泄漏的原因
本篇文章主要介紹了Android Studio 3.0上分析內(nèi)存泄漏的原因,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11詳解Android冷啟動實(shí)現(xiàn)APP秒開的方法
這篇文章給大家介紹的是Android冷啟動實(shí)現(xiàn)APP秒開的方法,對大家日常開發(fā)APP還是很實(shí)用的,有需要的可以參考借鑒。2016-08-08詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)
這篇文章主要介紹了Runtime解決屏幕旋轉(zhuǎn)問題的方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android學(xué)習(xí)筆記(二)App工程文件分析
之前寫過一篇關(guān)于安卓環(huán)境配置以及第一個(gè)app的制作過程,下面我們來進(jìn)一步,分析下APP工程文件2014-07-07Mac中配置gradle環(huán)境及使用android studio打包jar包與arr包的方法
這篇文章主要給大家介紹了關(guān)于在Mac中配置gradle環(huán)境,以及使用android studio打包jar包與arr包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解
這篇文章主要介紹了Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-09-09Android實(shí)現(xiàn)圖片滾動和頁簽控件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android實(shí)現(xiàn)圖片滾動控件含頁簽功能的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05