欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android入門之TextClock的使用教程

 更新時(shí)間:2022年11月11日 09:09:17   作者:TGITCIC  
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個(gè)控件。本文將為大家詳細(xì)說說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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論