目标:
掌握时间选择器的使用;
可以通过程序程序配置显示的时间;
内容:
时间选择器:TimePicker,主要的目的是可以进行时间的调整,此类的继承结构如下:
在Android中使用TimePicker(时间选择器),可以进行时间的快速调整。此类定义如下:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout 帧布局
↳ android.widget.TimePicker
下面通过简单的程序观察如何使用时间选择器
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TimePicker 定义了时间选择器 android:id="@+id/tp1" android:layout_width="fill_parent" android:layout_height="wrap_content"
/> </LinearLayout>
|
因为现在的语言是中文,如果现在换成了英文,则变为am/pm。
如果要想操作时间选择器本身也提供了一些操作或者说是时间调整的方法,默认情况下时间是采用12小时制的。
下面通过程序调整为24小时制。
<TimePicker android:id="@+id/tp2" android:layout_width="fill_parent" android:layout_height="wrap_content"
/> |
在activity程序之中要进行以下代码的编写:
package com.example.TimePickerProject;
import android.app.Activity; import android.os.Bundle; import android.widget.TimePicker;
public class MyTimePicker extends Activity { private TimePicker mytp = null ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.mytp = (TimePicker) super.findViewById(R.id.tp2); //取得组件 this.mytp.setIs24HourView(true); //设置为24小时制 this.mytp.setCurrentHour(10); //设置时 this.mytp.setCurrentMinute(30); //设置分
} } |
小结:
时间选择器可以直接进行时间的显示调整;
时间的显示也是和语言有关,即和国际化有关。