DateTimePicker
2016/1/29 16:02:08
1. 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/dateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间显示"/>
</LinearLayout>
<Button android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变日期"/>
<Button android:id="@+id/pickTime12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变时间(12小时制)"/>
<Button android:id="@+id/pickTime24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变时间24小时制"/>
</LinearLayout>
2. 配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lgq.datewidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Material" > //Material Design设计风格
<activity
android:name=".DateWidgets1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. 实现
public class DateWidgets1 extends Activity {
// where we display the selected date and time
private TextView mDateDisplay;
// date and time
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int TIME_12_DIALOG_ID = 0;
static final int TIME_24_DIALOG_ID = 1;
static final int DATE_DIALOG_ID = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_widgets_example_1);
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
setDialogOnClickListener(R.id.pickDate, DATE_DIALOG_ID);
setDialogOnClickListener(R.id.pickTime12, TIME_12_DIALOG_ID);
setDialogOnClickListener(R.id.pickTime24, TIME_24_DIALOG_ID);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateDisplay();
}
private void setDialogOnClickListener(int buttonId, final int dialogId) {
Button b = (Button) findViewById(buttonId);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(dialogId);
}
});
}
/**
* 创建Dialog
*/
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_12_DIALOG_ID:
case TIME_24_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, id == TIME_24_DIALOG_ID);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
/**
* 显示Dialog
*/
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case TIME_12_DIALOG_ID:
case TIME_24_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" ")
.append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
/**
* 日期设置监听
*/
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
/**
* 时间设置监听
*/
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
4. 效果
日期选择
时间选择
- 12小时制
- 24小时制