Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎ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)入后臺(tái),暫停計(jì)時(shí)
@Override
protected void onStop() {
super.onStop();
wasRunning = running;
running = false;
}
//重新進(jìn)入app,開(kāi)始計(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),重新開(kāi)始計(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線程更新界面,不能在后臺(tái)線程更新界面
*
* ** 使用ui線程的Handler定時(shí)更新 **
* 將任務(wù)封裝到 Runnable的run方法中 ,通過(guò)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);
}
}
);
}
}
測(cè)試

完成
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android RecycleView 實(shí)現(xiàn)左滑上下分層示例代碼(自定義功能)
這篇文章主要介紹了Android RecycleView 實(shí)現(xiàn)左滑上下分層示例代碼(自定義功能),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)算器功能,計(jì)算表達(dá)式,能計(jì)算小數(shù)點(diǎn)以及括號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)氣泡布局/彈窗效果,可控制氣泡尖角方向及偏移量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android自定義viewgroup快速滑動(dòng)(4)
這篇文章主要為大家詳細(xì)介紹了Android自定義viewgroup快速滑動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android 藍(lán)牙開(kāi)發(fā)實(shí)例解析
本文主要介紹Android 藍(lán)牙開(kāi)發(fā),這里提供實(shí)例代碼和詳細(xì)解析實(shí)現(xiàn)方法,對(duì)開(kāi)發(fā)Android藍(lán)牙開(kāi)發(fā)的朋友提供簡(jiǎn)單示例,有需要的朋友可以參考下2016-08-08
Android頁(yè)面中引導(dǎo)蒙層的使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android頁(yè)面中的引導(dǎo)蒙層使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Android 屏幕實(shí)現(xiàn)上下翻轉(zhuǎn)
這篇文章主要介紹了Android 屏幕實(shí)現(xiàn)上下翻轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2017-07-07

