Android Date & Time組件(上)

2023-03-31 13:41 更新

本節(jié)引言:

本節(jié)給大家?guī)淼氖茿ndroid給我們提供的顯示時(shí)間的幾個(gè)控件,他們分別是: TextClock,AnalogClock,Chronometer,另外其實(shí)還有個(gè)過時(shí)的DigitalClock就不講解了! 好的,開始本節(jié)內(nèi)容!


1.TextClock(文本時(shí)鐘)

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);

另外他給我們提供了下面這些方法,對(duì)應(yīng)的還有g(shù)et方法:

Attribute Name Related Method Description
android:format12Hour setFormat12Hour(CharSequence) 設(shè)置12時(shí)制的格式
android:format24Hour setFormat24Hour(CharSequence) 設(shè)置24時(shí)制的格式
android:timeZone setTimeZone(String) 設(shè)置時(shí)區(qū)

其實(shí)更多的時(shí)間我們是花在時(shí)間形式定義上,就是里面這個(gè)CharSequence! 這里提供下常用的寫法以及結(jié)果:

<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MMMM dd, yyyy h:mmaa"/>
    <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"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="Noteworthy day: 'M/d/yy"/>

運(yùn)行結(jié)果:

PS:另外minsdk 要大于或者等于17哦!


2.AnalogClock(模擬時(shí)鐘)

就是下圖這種:

官網(wǎng)中我們可以看到這樣三個(gè)屬性:

依次是:表背景,表時(shí)針,分時(shí)針的圖片,我們可以自行定制:

示例代碼如下:

<AnalogClock
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:dial="@mipmap/ic_c_bg"
        android:hand_hour="@mipmap/zhen_shi"
        android:hand_minute="@mipmap/zhen_fen" />

運(yùn)行結(jié)果:


3.Chronometer(計(jì)時(shí)器)

如題,就是一個(gè)簡(jiǎn)單的計(jì)時(shí)器,我們直接上使用示例吧:

使用示例:

實(shí)現(xiàn)代碼:

布局代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#ff0000"
        android:textSize="60dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnStart"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="開始記時(shí)" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止記時(shí)" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重置" />

        <Button
            android:id="@+id/btn_format"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="格式化" />
    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{

    private Chronometer chronometer;
    private Button btn_start,btn_stop,btn_base,btn_format;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        chronometer = (Chronometer) findViewById(R.id.chronometer);
        btn_start = (Button) findViewById(R.id.btnStart);
        btn_stop = (Button) findViewById(R.id.btnStop);
        btn_base = (Button) findViewById(R.id.btnReset);
        btn_format = (Button) findViewById(R.id.btn_format);

        chronometer.setOnChronometerTickListener(this);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_base.setOnClickListener(this);
        btn_format.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnStart:
                chronometer.start();// 開始計(jì)時(shí)
                break;
            case R.id.btnStop:
                chronometer.stop();// 停止計(jì)時(shí)
                break;
            case R.id.btnReset:
                chronometer.setBase(SystemClock.elapsedRealtime());// 復(fù)位
                break;
            case R.id.btn_format:
                chronometer.setFormat("Time:%s");// 更改時(shí)間顯示格式
                break;
        }
    }

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        String time = chronometer.getText().toString();
        if(time.equals("00:00")){
            Toast.makeText(MainActivity.this,"時(shí)間到了~",Toast.LENGTH_SHORT).show();
        }
    }
}

運(yùn)行截圖:


本節(jié)小結(jié):

本節(jié)跟大家簡(jiǎn)單的介紹了TextClock,AnalogClock,Chronometer這三個(gè)組件,從篇幅就可以看出 其實(shí)這幾個(gè)東西用得并不多,幾乎是沒用過...知道下就好,用法也超簡(jiǎn)單... 就這樣吧,本節(jié)就到這里~謝謝

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)