W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
本節(jié)給大家?guī)淼氖茿ndroid給我們提供的顯示時(shí)間的幾個(gè)控件,他們分別是: TextClock,AnalogClock,Chronometer,另外其實(shí)還有個(gè)過時(shí)的DigitalClock就不講解了! 好的,開始本節(jié)內(nèi)容!
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)制模式中:
另外他給我們提供了下面這些方法,對(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哦!
就是下圖這種:
官網(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é)果:
如題,就是一個(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ǎn)單的介紹了TextClock,AnalogClock,Chronometer這三個(gè)組件,從篇幅就可以看出 其實(shí)這幾個(gè)東西用得并不多,幾乎是沒用過...知道下就好,用法也超簡(jiǎn)單... 就這樣吧,本節(jié)就到這里~謝謝
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: