Android入門之TextClock的使用教程
介紹
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個(gè)控件。
TextClock可以以字符串格式顯示當(dāng)前的日期和時(shí)間,因此推薦在Android 4.2以后使用TextClock。
這個(gè)控件推薦在24進(jìn)制的android系統(tǒng)中使用,TextClock提供了兩種不同的格式, 一種是在24進(jìn)制中顯示時(shí)間和日期,另一種是在12進(jìn)制中顯示時(shí)間和日期。大部分人喜歡默認(rèn)的設(shè)置。
可以通過調(diào)用:TextClock提供的is24HourModeEnabled()方法來查看,系統(tǒng)是否在使用24進(jìn)制時(shí)間顯示! 在24進(jìn)制模式中:
- 如果沒獲取時(shí)間,首先通過getFormat24Hour()返回值;
- 獲取失敗則通過getFormat12Hour()獲取返回值;
- 以上都獲取失敗則使用默認(rèn);
它的使用非常簡(jiǎn)單。
課程例子
我們通過5種格式來說明一下這個(gè)TextClock的使用。
在例子中,我們做了一個(gè)按鈕,這個(gè)按鈕會(huì)對(duì)第3行的TextClock根據(jù)系統(tǒng)是否Enable24 Hour來把它的顯示改成:24小時(shí)的顯示格式。
UI主界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MM/dd/yy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MMM dd, yyyy h:mmaa"/> <TextClock android:id="@+id/timeTextClock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MMMM dd, yyyy h:mm:aa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="E, MMMM dd, yyyy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/> <Button android:id="@+id/setTimeButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
代碼
package org.mk.android.demo.demotxtclock; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextClock; public class MainActivity extends AppCompatActivity { private TextClock timeClock; private Button setTimeButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTimeButton = (Button) findViewById(R.id.setTimeButton); timeClock = (TextClock) findViewById(R.id.timeTextClock); setTimeButton.setOnClickListener(new OnClickListener()); } private class OnClickListener implements View.OnClickListener { @Override public void onClick(View v) { String clockFormatter = "MMMM dd, yyyy h:mm:ss"; if (timeClock.is24HourModeEnabled()) { Log.i("app", ">>>>>>System has been enabled the 24Hour Model"); timeClock.setFormat24Hour(clockFormatter); } else { Log.i("app", ">>>>>>System has not been enabled the 24Hour " + "Model"); timeClock.setFormat12Hour(clockFormatter); } } } }
運(yùn)行效果
當(dāng)我們點(diǎn)下了按鈕后,可以看到界面上第三行的TextClock顯示的值發(fā)生了變化,如下截圖。
自己動(dòng)一下手試試看效果吧。
到此這篇關(guān)于Android入門之TextClock的使用教程的文章就介紹到這了,更多相關(guān)Android TextClock內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能
- Android自定義DigitalClock控件實(shí)現(xiàn)商品倒計(jì)時(shí)
- Android ScreenLockReceiver監(jiān)聽鎖屏功能示例
- Android應(yīng)用程序保持后臺(tái)喚醒(使用WakeLock實(shí)現(xiàn))
- Android DigitalClock組件用法實(shí)例
- Android AnalogClock簡(jiǎn)單使用方法實(shí)例
- Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實(shí)例詳解
- Android控件之AnalogClock與DigitalClock用法實(shí)例分析
- Android Lock鎖實(shí)現(xiàn)原理詳細(xì)分析
相關(guān)文章
Android 中SQLite技術(shù)實(shí)例詳解
這篇文章主要介紹了Android 中SQLite技術(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android SQLite數(shù)據(jù)庫基本操作方法
本篇文章主要介紹了Android SQLite數(shù)據(jù)庫基本操作方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Kotlin 創(chuàng)建接口或者抽象類的匿名對(duì)象實(shí)例
這篇文章主要介紹了Kotlin 創(chuàng)建接口或者抽象類的匿名對(duì)象實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2017-03-03Android如何讓APP無法在指定的系統(tǒng)版本上運(yùn)行(實(shí)現(xiàn)方法)
這篇文章主要介紹了Android如何讓APP無法在指定的系統(tǒng)版本上運(yùn)行(實(shí)現(xiàn)方法),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Flutter中g(shù)o_router路由管理的使用指南
go_router?是一個(gè)?Flutter?的第三方路由插件,相比?Flutter?自帶的路由,go_router?更加靈活,而且簡(jiǎn)單易用,下面小編就來和大家聊聊go_router的使用吧2023-08-08