下面通过一个具体的实例来说明日期、时间选择器的共体应用。
例如在Eclipse中创建安卓 I项目,名称随意.在屏箱中添加日期、时间拾取。并实现在改变日期或时间时。通过消息提示框显示改变后的日期或时间。
1)布局文件中的关键代码:
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2)在主活动MainActivity的onCreateO方法中,获取日期拾取组件和时间拾取组件,并将时间拾取组件设2为24小时制式显示,具休代码如下:
// 获取日期拾取器
DatePicker datepicker = (DatePicker) findViewById(R.id.datepicker);
//获取时间拾取组件
TimePicker timepicker = (TimePicker) findViewById(R.id.timepicker);
timepicker.setIs24HourView(true);
3)创建一个日历对象。井获取当前年、月、日、小时和分钟,具体代码如下:
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
下面贴出完整代码:
xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期时间拾取器" />
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java代码
package com.xzy.demo.dateandtime;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class Date_Time_pluginActivity extends Activity {
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);
timepicker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
timepicker.setCurrentHour(hour);
timepicker.setCurrentMinute(minute);
// datapicker初始化
datepicker.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Date_Time_pluginActivity.this.year = year;// 改变year属性的值
Date_Time_pluginActivity.this.month = monthOfYear;// 改变month属性的值
Date_Time_pluginActivity.this.day = dayOfMonth;// 改变day属性的值
String date_time = year + "年" + (month + 1) + "月" + day + "日";
Toast.makeText(Date_Time_pluginActivity.this, date_time, 2000)
.show();
}
});
timepicker.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Date_Time_pluginActivity.this.hour = hourOfDay;// 改变hour属性的值
Date_Time_pluginActivity.this.minute = minute;// 改变minute属性的值
String time = year + "年" + (month + 1) + "月" + day + "日" + hour
+ "时" + minute + "分";
Toast.makeText(Date_Time_pluginActivity.this, time, 2000)
.show();
}
});
}
}
改变日期时截图:
改变时间时: