Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí)
Android提供了實(shí)現(xiàn)按照秒計(jì)時(shí)的API,今天就是用這個(gè)API實(shí)現(xiàn)簡單的倒計(jì)時(shí)。
來個(gè)布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Chronometer android:id="@+id/myChronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始計(jì)時(shí)" /> <Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> <Button android:id="@+id/btnBase" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="復(fù)位" /> <Button android:id="@+id/btnFormat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="格式化" /> </LinearLayout> </LinearLayout>
對(duì)應(yīng)活動(dòng)中的代碼如下:
package com.example.timer; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class MainActivity extends Activity { private Chronometer myChronometer = null; private Button btnStart = null; private Button btnStop = null; private Button btnBase = null; private Button btnFormat = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.activity_main); // 設(shè)置要使用的布局管理器 this.myChronometer = (Chronometer) super .findViewById(R.id.myChronometer); btnStart = (Button) super.findViewById(R.id.btnStart); btnStop = (Button) super.findViewById(R.id.btnStop); btnBase = (Button) super.findViewById(R.id.btnBase); btnFormat = (Button) super.findViewById(R.id.btnFormat); btnStart.setOnClickListener(new OnClickListenerStart()); btnStop.setOnClickListener(new OnClickListenerStop()); btnBase.setOnClickListener(new OnClickListenerBase()); btnFormat.setOnClickListener(new OnClickListenerFormat()); } private class OnClickListenerStart implements OnClickListener { public void onClick(View arg0) { myChronometer.start(); } } private class OnClickListenerStop implements OnClickListener { public void onClick(View arg0) { myChronometer.stop(); } } private class OnClickListenerBase implements OnClickListener { public void onClick(View arg0) { myChronometer.setBase(SystemClock.elapsedRealtime()); } } private class OnClickListenerFormat implements OnClickListener { public void onClick(View arg0) { myChronometer.setFormat("新的顯示格式:%s。"); } } }
運(yùn)行跑起來看看效果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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控件Chronometer定時(shí)器的實(shí)現(xiàn)方法
- Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
- Android利用Chronometer實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- Android編程之計(jì)時(shí)器Chronometer簡單示例
相關(guān)文章
Android簡單的短信驗(yàn)證功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android簡單的短信驗(yàn)證功能的實(shí)現(xiàn)代碼,本文是小編使用sdk過程的一些心得,需要的朋友可以參考下2018-07-07Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解
這篇文章主要介紹了Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android實(shí)現(xiàn)左上角(其他邊角)傾斜的標(biāo)簽(環(huán)繞效果)效果
這篇文章主要介紹了Android實(shí)現(xiàn)左上角(其他邊角)傾斜的標(biāo)簽(環(huán)繞效果)效果,本文通過圖文實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Android入門之TableLayout應(yīng)用解析(一)
這篇文章主要介紹了Android入門之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08Android開發(fā)筆記之:對(duì)實(shí)踐TDD的一些建議說明
本篇文章是對(duì)Android中實(shí)踐TDD的一些建議進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android布局中margin與padding的區(qū)別及說明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01