Android入門之TextClock的使用教程
介紹
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個控件。
TextClock可以以字符串格式顯示當前的日期和時間,因此推薦在Android 4.2以后使用TextClock。
這個控件推薦在24進制的android系統(tǒng)中使用,TextClock提供了兩種不同的格式, 一種是在24進制中顯示時間和日期,另一種是在12進制中顯示時間和日期。大部分人喜歡默認的設(shè)置。
可以通過調(diào)用:TextClock提供的is24HourModeEnabled()方法來查看,系統(tǒng)是否在使用24進制時間顯示! 在24進制模式中:
- 如果沒獲取時間,首先通過getFormat24Hour()返回值;
- 獲取失敗則通過getFormat12Hour()獲取返回值;
- 以上都獲取失敗則使用默認;
它的使用非常簡單。
課程例子
我們通過5種格式來說明一下這個TextClock的使用。
在例子中,我們做了一個按鈕,這個按鈕會對第3行的TextClock根據(jù)系統(tǒng)是否Enable24 Hour來把它的顯示改成:24小時的顯示格式。
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); } } } }
運行效果
當我們點下了按鈕后,可以看到界面上第三行的TextClock顯示的值發(fā)生了變化,如下截圖。
自己動一下手試試看效果吧。
到此這篇關(guān)于Android入門之TextClock的使用教程的文章就介紹到這了,更多相關(guān)Android TextClock內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android服務(wù)應(yīng)用ClockService實現(xiàn)鬧鐘功能
- Android自定義DigitalClock控件實現(xiàn)商品倒計時
- Android ScreenLockReceiver監(jiān)聽鎖屏功能示例
- Android應(yīng)用程序保持后臺喚醒(使用WakeLock實現(xiàn))
- Android DigitalClock組件用法實例
- Android AnalogClock簡單使用方法實例
- Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實例詳解
- Android控件之AnalogClock與DigitalClock用法實例分析
- Android Lock鎖實現(xiàn)原理詳細分析
相關(guān)文章
Android SQLite數(shù)據(jù)庫基本操作方法
本篇文章主要介紹了Android SQLite數(shù)據(jù)庫基本操作方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Kotlin 創(chuàng)建接口或者抽象類的匿名對象實例
這篇文章主要介紹了Kotlin 創(chuàng)建接口或者抽象類的匿名對象實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android Retrofit和Rxjava的網(wǎng)絡(luò)請求
這篇文章主要介紹了Android Retrofit和Rxjava的網(wǎng)絡(luò)請求的相關(guān)資料,需要的朋友可以參考下2017-03-03Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法)
這篇文章主要介紹了Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Flutter中g(shù)o_router路由管理的使用指南
go_router?是一個?Flutter?的第三方路由插件,相比?Flutter?自帶的路由,go_router?更加靈活,而且簡單易用,下面小編就來和大家聊聊go_router的使用吧2023-08-08