Android計(jì)時(shí)器控件Chronometer應(yīng)用實(shí)例
顯示一個(gè)計(jì)時(shí)器開(kāi)始計(jì)時(shí),當(dāng)計(jì)時(shí)器到達(dá)15s的時(shí)候,停止計(jì)時(shí)。此時(shí)頁(yè)面多一個(gè)重置按鈕,可再次進(jìn)行計(jì)時(shí)。
頁(yè)面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="@drawable/bg" > <Chronometer android:id="@+id/chronometer" android:layout_marginTop="8dp" android:layout_marginLeft="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/restart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" android:visibility="gone" /> </LinearLayout>
事件響應(yīng)
package com.example.chronometerdemo; import android.os.Bundle; import android.os.SystemClock; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; public class MainActivity extends Activity { Chronometer time=null; Button restart=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); time=(Chronometer) findViewById(R.id.chronometer); restart=(Button) findViewById(R.id.restart); //設(shè)置起始時(shí)間和時(shí)間格式,然后開(kāi)始計(jì)時(shí) time.setBase(SystemClock.elapsedRealtime()); time.setFormat("已用時(shí)間:%s"); time.start(); //給計(jì)時(shí)器添加監(jiān)聽(tīng)器,當(dāng)計(jì)時(shí)到達(dá)15s時(shí),要重置 time.setOnChronometerTickListener(new OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer arg0) { if(SystemClock.elapsedRealtime()-arg0.getBase()>=15000) { arg0.stop(); restart.setVisibility(View.VISIBLE); } } }); //給按鈕添加重置的效果 restart.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { time.setBase(SystemClock.elapsedRealtime()); time.start(); restart.setVisibility(View.GONE); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
運(yùn)行效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android編程之計(jì)時(shí)器Chronometer簡(jiǎn)單示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
- Android計(jì)時(shí)器chronometer使用實(shí)例講解
- 學(xué)習(xí)使用Android Chronometer計(jì)時(shí)器
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開(kāi)發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android 編程下的計(jì)時(shí)器代碼
相關(guān)文章
Android Studio 導(dǎo)入開(kāi)源項(xiàng)目的正確姿勢(shì)及注意事項(xiàng)
這篇文章主要介紹了Android Studio 導(dǎo)入開(kāi)源項(xiàng)目的正確姿勢(shì)及注意事項(xiàng),需要的朋友參考下吧2018-03-03Flutter利用SizeTransition實(shí)現(xiàn)組件飛入效果
本文將為大家介紹SizeTransition,SizeTransition用于更改子組件的尺寸來(lái)實(shí)現(xiàn)動(dòng)畫(huà),支持垂直方向或水平方向修改動(dòng)畫(huà)。本文將利用其實(shí)現(xiàn)組件飛入效果,需要的可以參考一下2022-04-04Flutter通過(guò)Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過(guò)Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android之利用EventBus發(fā)送消息傳遞示例
本篇文章主要介紹了Android之利用EventBus進(jìn)行消息傳遞示例。EventBus是一款針對(duì)Android優(yōu)化的發(fā)布/訂閱事件總線,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)?lái)一篇詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Android編程獲取網(wǎng)絡(luò)時(shí)間實(shí)例分析
這篇文章主要介紹了Android編程獲取網(wǎng)絡(luò)時(shí)間,結(jié)合實(shí)例形式對(duì)比分析了Android通過(guò)訪問(wèn)網(wǎng)絡(luò)及通過(guò)GPS獲取網(wǎng)絡(luò)時(shí)間的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果
這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時(shí)間軸的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11