[关闭]
@coder-pig 2015-08-25T06:57:35.000000Z 字数 4498 阅读 1471

Android基础入门教程——2.3.11 Date & Time组件(上)

Android基础入门教程


本节引言:

本节给大家带来的是Android给我们提供的显示时间的几个控件,他们分别是:
TextClock,AnalogClock,Chronometer,另外其实还有个过时的DigitalClock就不讲解了!
好的,开始本节内容!


1.TextClock(文本时钟)

TextClock是在Android 4.2(API 17)后推出的用来替代DigitalClock的一个控件!
TextClock可以以字符串格式显示当前的日期和时间,因此推荐在Android 4.2以后使用TextClock。
这个控件推荐在24进制的android系统中使用,TextClock提供了两种不同的格式,
一种是在24进制中显示时间和日期,另一种是在12进制中显示时间和日期。大部分人喜欢默认的设置。

可以通过调用:TextClock提供的is24HourModeEnabled()方法来查看,系统是否在使用24进制时间显示!
在24进制模式中:

另外他给我们提供了下面这些方法,对应的还有get方法:

Attribute Name Related Method Description
android:format12Hour setFormat12Hour(CharSequence) 设置12时制的格式
android:format24Hour setFormat24Hour(CharSequence) 设置24时制的格式
android:timeZone setTimeZone(String) 设置时区

其实更多的时间我们是花在时间形式定义上,就是里面这个CharSequence!
这里提供下常用的写法以及结果:

  1. <TextClock
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:format12Hour="MM/dd/yy h:mmaa"/>
  5. <TextClock
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:format12Hour="MMM dd, yyyy h:mmaa"/>
  9. <TextClock
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:format12Hour="MMMM dd, yyyy h:mmaa"/>
  13. <TextClock
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:format12Hour="E, MMMM dd, yyyy h:mmaa"/>
  17. <TextClock
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/>
  21. <TextClock
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:format12Hour="Noteworthy day: 'M/d/yy"/>

运行结果:

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


2.AnalogClock(模拟时钟)

就是下图这种:

官网中我们可以看到这样三个属性:

依次是:表背景,表时针,分时针的图片,我们可以自行定制:

示例代码如下:

  1. <AnalogClock
  2. android:layout_width="100dp"
  3. android:layout_height="100dp"
  4. android:dial="@mipmap/ic_c_bg"
  5. android:hand_hour="@mipmap/zhen_shi"
  6. android:hand_minute="@mipmap/zhen_fen" />

运行结果:


3.Chronometer(计时器)

如题,就是一个简单的计时器,我们直接上使用示例吧:

使用示例:

实现代码:
布局代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity">
  7. <Chronometer
  8. android:id="@+id/chronometer"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:gravity="center"
  12. android:textColor="#ff0000"
  13. android:textSize="60dip" />
  14. <LinearLayout
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_margin="10dip"
  18. android:orientation="horizontal">
  19. <Button
  20. android:id="@+id/btnStart"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:text="开始记时" />
  25. <Button
  26. android:id="@+id/btnStop"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="停止记时" />
  31. <Button
  32. android:id="@+id/btnReset"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_weight="1"
  36. android:text="重置" />
  37. <Button
  38. android:id="@+id/btn_format"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="格式化" />
  42. </LinearLayout>
  43. </LinearLayout>

MainActivity.java

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{
  2. private Chronometer chronometer;
  3. private Button btn_start,btn_stop,btn_base,btn_format;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. initView();
  9. }
  10. private void initView() {
  11. chronometer = (Chronometer) findViewById(R.id.chronometer);
  12. btn_start = (Button) findViewById(R.id.btnStart);
  13. btn_stop = (Button) findViewById(R.id.btnStop);
  14. btn_base = (Button) findViewById(R.id.btnReset);
  15. btn_format = (Button) findViewById(R.id.btn_format);
  16. chronometer.setOnChronometerTickListener(this);
  17. btn_start.setOnClickListener(this);
  18. btn_stop.setOnClickListener(this);
  19. btn_base.setOnClickListener(this);
  20. btn_format.setOnClickListener(this);
  21. }
  22. @Override
  23. public void onClick(View v) {
  24. switch (v.getId()){
  25. case R.id.btnStart:
  26. chronometer.start();// 开始计时
  27. break;
  28. case R.id.btnStop:
  29. chronometer.stop();// 停止计时
  30. break;
  31. case R.id.btnReset:
  32. chronometer.setBase(SystemClock.elapsedRealtime());// 复位
  33. break;
  34. case R.id.btn_format:
  35. chronometer.setFormat("Time:%s");// 更改时间显示格式
  36. break;
  37. }
  38. }
  39. @Override
  40. public void onChronometerTick(Chronometer chronometer) {
  41. String time = chronometer.getText().toString();
  42. if(time.equals("00:00")){
  43. Toast.makeText(MainActivity.this,"时间到了~",Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46. }

运行截图:


本节小结:

本节跟大家简单的介绍了TextClock,AnalogClock,Chronometer这三个组件,从篇幅就可以看出
其实这几个东西用得并不多,几乎是没用过...知道下就好,用法也超简单...
就这样吧,本节就到这里~谢谢

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注