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