Android實戰(zhàn)教程第四十篇之Chronometer實現(xiàn)倒計時
更新時間:2016年11月10日 10:57:08 作者:楊道龍
這篇文章主要介紹了Android實戰(zhàn)教程第四十篇之Chronometer實現(xiàn)倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Android提供了實現(xiàn)按照秒計時的API,今天就是用這個API實現(xiàn)簡單的倒計時。
來個布局:
<?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計時器的三種實現(xiàn)方式(Chronometer、Timer、handler)
- Android Chronometer控件實現(xiàn)計時器函數(shù)詳解
- Android計時器chronometer使用實例講解
- 學習使用Android Chronometer計時器
- android之計時器(Chronometer)的使用以及常用的方法
- Android控件Chronometer定時器的實現(xiàn)方法
- Android自帶倒計時控件Chronometer使用方法詳解
- Android利用Chronometer實現(xiàn)倒計時功能
- Android自定義Chronometer實現(xiàn)短信驗證碼秒表倒計時功能
- Android時分秒計時器的兩種實現(xiàn)方法
- Android編程之計時器Chronometer簡單示例
相關文章
android使用TextView實現(xiàn)跑馬燈效果
這篇文章主要為大家詳細介紹了android使用TextView實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android實現(xiàn)類似360,QQ管家那樣的懸浮窗
用到的就是WindowManager以及WindowManager.LayoutParams,對這個LayoutParams做文章,當設置為屬性后,然后,創(chuàng)建一個View,將這個View添加到WindowManager中就行2013-06-06
Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能
本篇文章主要介紹了Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能,非常具有實用價值,需要的朋友可以參考下2017-10-10
Android開發(fā)之DatePickerDialog、TimePickerDialog時間日期對話框用法示例
這篇文章主要介紹了Android開發(fā)之DatePickerDialog、TimePickerDialog時間日期對話框用法,結合實例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時間相關操作技巧,需要的朋友可以參考下2019-03-03

