Android入門之計時器Chronometer的使用教程
介紹
非常簡單的一個計時器,沒有太多原理,我們直接上代碼。
先看課程目標(biāo)
課程目標(biāo)
就是一個簡單的計時器,我們直接上使用示例吧
界面里有一個計時器,4個按鈕。
- 開始計時,上面這個計時器就開始讀秒;
- 停止計時,計時器會暫停計時;
- 重置,計時器會歸零;
- 變格式,計時器會變成:Time:%s"的格式顯示;
界面端代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Chronometer android:id="@+id/chronometer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#ff0000" android:textSize="60dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:orientation="horizontal"> <Button android:id="@+id/btnStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="開始記時" /> <Button android:id="@+id/btnStop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止記時" /> <Button android:id="@+id/btnReset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="重置" /> <Button android:id="@+id/btnFormat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="變格式" /> </LinearLayout> </LinearLayout>
后端交互代碼
package org.mk.android.demo.demochrometer; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Chronometer; public class MainActivity extends AppCompatActivity { private Chronometer chronometer; private Button btnStart,btnStop,btnReset,btnFormat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnFormat=(Button) findViewById(R.id.btnFormat); btnStart=(Button) findViewById(R.id.btnStart); btnStop=(Button) findViewById(R.id.btnStop); btnReset=(Button) findViewById(R.id.btnReset); btnStart.setOnClickListener(new OnClickListener()); btnStop.setOnClickListener(new OnClickListener()); btnReset.setOnClickListener(new OnClickListener()); btnFormat.setOnClickListener(new OnClickListener()); chronometer=(Chronometer) findViewById(R.id.chronometer); } private class OnClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnStart: chronometer.start();// 開始計時 break; case R.id.btnStop: chronometer.stop();// 停止計時 break; case R.id.btnReset: chronometer.setBase(SystemClock.elapsedRealtime());// 復(fù)位 break; case R.id.btnFormat: Log.i("app","into formatter"); chronometer.setFormat("Time:%s");// 更改時間顯示格式 break; } } } }
運(yùn)行效果
以上是按下了【變格式】按鈕后顯示的變化,自己去動動手試一下唄。
到此這篇關(guān)于Android入門之計時器Chronometer的使用教程的文章就介紹到這了,更多相關(guān)Android計時器Chronometer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Jetpack導(dǎo)航組件Navigation創(chuàng)建使用詳解
這篇文章主要為大家介紹了Android Jetpack導(dǎo)航組件Navigation創(chuàng)建及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變的實現(xiàn)
這篇文章主要介紹了Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android自定義WheelView地區(qū)選擇三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Android自定義WheelView地區(qū)選擇三級聯(lián)動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android用RecyclerView實現(xiàn)圖標(biāo)拖拽排序以及增刪管理
這篇文章主要介紹了Android用RecyclerView實現(xiàn)圖標(biāo)拖拽排序以及增刪管理的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03Android實現(xiàn)第三方授權(quán)登錄、分享以及獲取用戶資料
本篇文章介紹了Android實現(xiàn)第三方授權(quán)登錄、分享以及獲取用戶資料,詳細(xì)的介紹了第三方授權(quán)登錄的實現(xiàn)代碼,有需要的朋友可以了解一下。2016-11-11Android實現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法
這篇文章主要介紹了Android實現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法,很實用的功能,需要的朋友可以參考下2014-07-07Android自定義view實現(xiàn)帶header和footer的Layout
這篇文章主要介紹了Android自定義view實現(xiàn)帶header和footer的Layout,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02Android開發(fā)實現(xiàn)的Intent跳轉(zhuǎn)工具類實例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的Intent跳轉(zhuǎn)工具類,簡單描述了Intent組件的功能并結(jié)合實例形式給出了頁面跳轉(zhuǎn)、拍照、圖片調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11