Date & Time組件(上)
本節(jié)引言:
本節(jié)給大家?guī)淼氖茿ndroid給我們提供的顯示時間的幾個控件,他們分別是: TextClock,AnalogClock,Chronometer,另外其實還有個過時的DigitalClock就不講解了! 好的,開始本節(jié)內(nèi)容!
1.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()獲取返回值;
- 以上都獲取失敗則使用默認;
另外他給我們提供了下面這些方法,對應(yīng)的還有g(shù)et方法:
Attribute Name | Related Method | Description |
---|---|---|
android:format12Hour | setFormat12Hour(CharSequence) | 設(shè)置12時制的格式 |
android:format24Hour | setFormat24Hour(CharSequence) | 設(shè)置24時制的格式 |
android:timeZone | setTimeZone(String) | 設(shè)置時區(qū) |
其實更多的時間我們是花在時間形式定義上,就是里面這個CharSequence! 這里提供下常用的寫法以及結(jié)果:
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"/>
運行結(jié)果:
PS:另外minsdk 要大于或者等于17哦!
2.AnalogClock(模擬時鐘)
就是下圖這種:
官網(wǎng)中我們可以看到這樣三個屬性:
依次是:表背景,表時針,分時針的圖片,我們可以自行定制:
示例代碼如下:
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" />
運行結(jié)果:
3.Chronometer(計時器)
如題,就是一個簡單的計時器,我們直接上使用示例吧:
使用示例:
實現(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="開始記時" />
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止記時" />
<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();// 開始計時
break;
case R.id.btnStop:
chronometer.stop();// 停止計時
break;
case R.id.btnReset:
chronometer.setBase(SystemClock.elapsedRealtime());// 復(fù)位
break;
case R.id.btn_format:
chronometer.setFormat("Time:%s");// 更改時間顯示格式
break;
}
}
@Override
public void onChronometerTick(Chronometer chronometer) {
String time = chronometer.getText().toString();
if(time.equals("00:00")){
Toast.makeText(MainActivity.this,"時間到了~",Toast.LENGTH_SHORT).show();
}
}
}
運行截圖:
本節(jié)小結(jié):
本節(jié)跟大家簡單的介紹了TextClock,AnalogClock,Chronometer這三個組件,從篇幅就可以看出 其實這幾個東西用得并不多,幾乎是沒用過...知道下就好,用法也超簡單... 就這樣吧,本節(jié)就到這里~謝謝