android学习笔记21:日期时间控件
android中的日期控件为:DatePicker,时间控件为TimePicker。通过指定两个易做图OnDateChangelistner和OnTimeChangeListner就可以监控这两个控件的改变,从而得到相应的时间。
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择您出生的具体时间"
/>
<!-- 定义一个DatePicker组件 -->
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<!-- 定义一个TimePicker组件 -->
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<!-- 显示用户输入日期、时间的控件 -->
<EditText android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"
/>
</LinearLayout>
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择您出生的具体时间"
/>
<!-- 定义一个DatePicker组件 -->
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<!-- 定义一个TimePicker组件 -->
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<!-- 显示用户输入日期、时间的控件 -->
<EditText android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"
/>
</LinearLayout>
public class ChooseDate extends Activity
{
//定义5个记录当前时间的变量
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
//获取当前的年、月、日、小时、分钟
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
//初始化DatePicker组件,初始化时指定易做图
datePicker.init(year , month ,day
, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker arg0, int year
, int month, int day)
{
ChooseDate.this.year = year;
ChooseDate.this.month = month;
ChooseDate.this.day = day;
//显示当前日期、时间
showDate(year, month , day , hour, minute);
}
});
//为TimePicker指定易做图
timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker arg0, int hour, int minute)
{
ChooseDate.this.hour = hour;
ChooseDate.this.minute = minute;
&nbs
补充:移动开发 , Android ,