Android實戰(zhàn)教程第四十篇之Chronometer實現倒計時
更新時間:2016年11月10日 10:57:08 作者:楊道龍
這篇文章主要介紹了Android實戰(zhàn)教程第四十篇之Chronometer實現倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Android提供了實現按照秒計時的API,今天就是用這個API實現簡單的倒計時。
來個布局:
<?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="開始計時" /> <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="復位" /> <Button android:id="@+id/btnFormat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="格式化" /> </LinearLayout> </LinearLayout>
對應活動中的代碼如下:
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); // 設置要使用的布局管理器 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。"); } } }
運行跑起來看看效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android計時器的三種實現方式(Chronometer、Timer、handler)
- Android Chronometer控件實現計時器函數詳解
- Android計時器chronometer使用實例講解
- 學習使用Android Chronometer計時器
- android之計時器(Chronometer)的使用以及常用的方法
- Android控件Chronometer定時器的實現方法
- Android自帶倒計時控件Chronometer使用方法詳解
- Android利用Chronometer實現倒計時功能
- Android自定義Chronometer實現短信驗證碼秒表倒計時功能
- Android時分秒計時器的兩種實現方法
- Android編程之計時器Chronometer簡單示例
相關文章
Android仿知乎客戶端關注和取消關注的按鈕點擊特效實現思路詳解
這篇文章主要介紹了Android仿知乎客戶端關注和取消關注的按鈕點擊特效實現思路詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android實現左上角(其他邊角)傾斜的標簽(環(huán)繞效果)效果
這篇文章主要介紹了Android實現左上角(其他邊角)傾斜的標簽(環(huán)繞效果)效果,本文通過圖文實例代碼相結合的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Android開發(fā)筆記之:對實踐TDD的一些建議說明
本篇文章是對Android中實踐TDD的一些建議進行了詳細的分析介紹,需要的朋友參考下2013-05-05Android布局中margin與padding的區(qū)別及說明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01