Android入門之TextClock的使用教程
介紹
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個控件。
TextClock可以以字符串格式顯示當前的日期和時間,因此推薦在Android 4.2以后使用TextClock。
這個控件推薦在24進制的android系統(tǒng)中使用,TextClock提供了兩種不同的格式, 一種是在24進制中顯示時間和日期,另一種是在12進制中顯示時間和日期。大部分人喜歡默認的設置。
可以通過調(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ā)生了變化,如下截圖。
自己動一下手試試看效果吧。

到此這篇關于Android入門之TextClock的使用教程的文章就介紹到這了,更多相關Android TextClock內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Android服務應用ClockService實現(xiàn)鬧鐘功能
- Android自定義DigitalClock控件實現(xiàn)商品倒計時
- Android ScreenLockReceiver監(jiān)聽鎖屏功能示例
- Android應用程序保持后臺喚醒(使用WakeLock實現(xiàn))
- Android DigitalClock組件用法實例
- Android AnalogClock簡單使用方法實例
- Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實例詳解
- Android控件之AnalogClock與DigitalClock用法實例分析
- Android Lock鎖實現(xiàn)原理詳細分析
相關文章
Android使用webView長按保存下載網(wǎng)絡圖片
這篇文章主要為大家詳細介紹了Android使用webView長按保存下載網(wǎng)絡圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
ListView下刷新刷功能相信從事Android開發(fā)的猿友們并不陌生,本文就帶領一些剛入門android的朋友或者一起愛分享的朋友來簡單的實現(xiàn)ListView的下拉刷新和左滑刪除效果。感興趣的朋友一起看看吧2016-11-11
android使用service和activity獲取屏幕尺寸的方法
這篇文章主要介紹了android使用service和activity獲取屏幕尺寸的方法,實例分析了基于service和activity兩種方法獲取屏幕尺寸的相關技巧,非常簡單實用,需要的朋友可以參考下2015-08-08
Android自定義組合控件之自定義下拉刷新和左滑刪除實例代碼
最近做了個項目,其中有項目需求,用到下拉刷新和左滑刪除,網(wǎng)上沒有找到比較理想的解決辦法。下面小編給大家分享我的一個小demo有關Android自定義組合控件之自定義下拉刷新和左滑刪除實例代碼,需要的朋友參考下2016-04-04
Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼的相關資料,需要的朋友可以參考下2016-04-04
Android EasyPermissions官方庫高效處理權限相關教程
Easypermissions簡化了Android M的運行時權限的申請、結果處理、判斷等步驟。這篇文章主要介紹了Android EasyPermissions官方庫高效處理權限相關教程,需要的朋友可以參考下2017-11-11

