Android實(shí)現(xiàn)簡單計(jì)時(shí)器功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單計(jì)時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下
布局
在res/layout 下進(jìn)行布局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="0dp"> <TextView android:text="00:00:00" android:textSize="60sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/timeView"/> <Button android:text="start" android:onClick="onClickStart" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/start"/> <Button android:text="stop" android:onClick="onClickStop" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/stop"/> <Button android:text="reset" android:onClick="onClickReset" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/reset"/> </LinearLayout> </android.support.constraint.ConstraintLayout>
MainActivity
package com.test; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private int seconds = 0; private boolean running = false; //計(jì)時(shí)狀態(tài) private boolean wasRunning = false; //保存running的狀態(tài) //app進(jìn)入后臺,暫停計(jì)時(shí) @Override protected void onStop() { super.onStop(); wasRunning = running; running = false; } //重新進(jìn)入app,開始計(jì)時(shí) @Override protected void onStart() { super.onStart(); if(wasRunning) running = true; } //失去焦點(diǎn)(如分屏),暫停計(jì)時(shí) @Override protected void onPause() { super.onPause(); wasRunning = running; running = false; } //獲得焦點(diǎn),重新開始計(jì)時(shí) @Override protected void onResume() { super.onResume(); if(wasRunning) running = true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取保存的狀態(tài) if(savedInstanceState!=null){ seconds = savedInstanceState.getInt("seconds"); running = savedInstanceState.getBoolean("running"); wasRunning = savedInstanceState.getBoolean("wasRunning"); } runTime(); } /** *保存狀態(tài) */ @Override public void onSaveInstanceState(Bundle saveInstanceState) { super.onSaveInstanceState(saveInstanceState); saveInstanceState.putInt("seconds",seconds); saveInstanceState.putBoolean("running",running); saveInstanceState.putBoolean("wasRunning",wasRunning); } /** * 響應(yīng)button的onClick事件 * 方法名和onClick的值一致 */ public void onClickStart(View button){ running = true; } public void onClickStop(View button){ running = false; } public void onClickReset(View button){ running = false; seconds = 0; } /** * 注意 ui線程不能被堵塞,因此不能在ui線程中調(diào)用sleep方法 * 只允許ui線程更新界面,不能在后臺線程更新界面 * * ** 使用ui線程的Handler定時(shí)更新 ** * 將任務(wù)封裝到 Runnable的run方法中 ,通過Handler的 * post(立即提交任務(wù))或postDelayed(實(shí)現(xiàn)定時(shí)調(diào)度)方法提交到ui線程 */ private void runTime(){ final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { final TextView textView = findViewById(R.id.timeView); int hour = seconds /3600%24; int minute = seconds%3600/60; String time = String.format("%02d:%02d:%02d",hour,minute,seconds%60); textView.setText(time); if(running) seconds++; handler.postDelayed(this,1000); } } ); } }
測試
完成
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android設(shè)計(jì)模式之Builder模式詳解
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之Builder模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android如何獲取屏幕、狀態(tài)欄及標(biāo)題欄的高度詳解
在日常開發(fā)中,經(jīng)常會遇到獲取屏幕高度、狀態(tài)欄高度等需求,所以下面這篇文章就給大家總結(jié)介紹了關(guān)于Android如何獲取屏幕、狀態(tài)欄及標(biāo)題欄高度的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考下。2017-10-10Android基于訊飛語音SDK實(shí)現(xiàn)語音識別
本例子是一個(gè)調(diào)用訊飛語音識別SDK的例子源碼是一個(gè)最純凈的Demo比較容易看懂。實(shí)現(xiàn)的是點(diǎn)擊按鈕開始語音監(jiān)聽,手機(jī)需要聯(lián)網(wǎng),2/3G的均可,希望本文對大家學(xué)習(xí)Android有所幫助2016-06-06Android實(shí)現(xiàn)多維商品屬性SKU選擇
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多維商品屬性SKU選擇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android系統(tǒng)進(jìn)程間通信Binder機(jī)制在應(yīng)用程序框架層的Java接口源代碼分析
本文主要介紹 Android系統(tǒng)進(jìn)程間通信Binder機(jī)制Java 接口源碼分析,這里詳細(xì)介紹了如何實(shí)現(xiàn)Binder 機(jī)制和Java接口直接的通信,有興趣的小伙伴可以參考下2016-08-08Android 自定義標(biāo)題欄的實(shí)例詳解
這篇文章主要介紹了 Android 自定義標(biāo)題欄的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣類似的功能,需要的朋友可以參考下2017-10-10Android開發(fā)筆記之:ListView刷新順序的問題詳解
本篇文章是對Android中ListView刷新順序的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android 自動判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用
本篇文章小編為大家介紹,在Android中 自動判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用。需要的朋友參考下2013-04-04Android Studio使用USB真機(jī)調(diào)試詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio使用USB真機(jī)調(diào)試的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05