網站首頁 編程語言 正文
介紹
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個控件。
TextClock可以以字符串格式顯示當前的日期和時間,因此推薦在Android 4.2以后使用TextClock。
這個控件推薦在24進制的android系統中使用,TextClock提供了兩種不同的格式, 一種是在24進制中顯示時間和日期,另一種是在12進制中顯示時間和日期。大部分人喜歡默認的設置。
可以通過調用:TextClock提供的is24HourModeEnabled()方法來查看,系統是否在使用24進制時間顯示! 在24進制模式中:
- 如果沒獲取時間,首先通過getFormat24Hour()返回值;
- 獲取失敗則通過getFormat12Hour()獲取返回值;
- 以上都獲取失敗則使用默認;
它的使用非常簡單。
課程例子
我們通過5種格式來說明一下這個TextClock的使用。
在例子中,我們做了一個按鈕,這個按鈕會對第3行的TextClock根據系統是否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顯示的值發生了變化,如下截圖。
自己動一下手試試看效果吧。
原文鏈接:https://blog.csdn.net/lifetragedy/article/details/127790628
相關推薦
- 2022-05-27 TensorFlow和Numpy矩陣操作中axis理解及axis=-1的解釋_python
- 2023-01-26 Kotlin協程Channel源碼示例淺析_Android
- 2022-08-12 python算法加密?pyarmor與docker_python
- 2022-12-29 Python利用卡方Chi特征檢驗實現提取關鍵文本特征_python
- 2022-12-08 React源碼state計算流程和優先級實例解析_React
- 2022-03-30 C語言中循環語句練習實例_C 語言
- 2022-08-03 Python?權限控制模塊?Casbin_python
- 2023-06-17 Flask中特殊裝飾器的使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支