MainActivity.java
package com.example.uidemo2;
import android.R.integer;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class MainActivity extends Activity {
//A view for selecting the time of day, in either 24 hour or AM/PM mode
TimePicker timePicker;
//This class is a widget for selecting a date.
//The date can be selected by a year, month, and day
DatePicker datePicker;
Button button1;
Button button2;
TimePickerDialog timePickerDialog;
DatePickerDialog datePickerDialog;
//如果想对TimePicker做设置得是使用OnTimeSetListener
OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timePicker.setCurrentHour(hourOfDay);
timePicker.setCurrentMinute(minute);
}
};
OnDateSetListener onDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
//datePicker设置年月日使用init方法
datePicker.init(year, monthOfYear, dayOfMonth, null);
}
};
private void fun2() {
/*
* android.app.DatePickerDialog.DatePickerDialog(Context context,
* OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
*
* Parameters:
* context The context the dialog is to run in.
* callBack How the parent is notified that the date is set.
* year The initial year of the dialog.
* monthOfYear The initial month of the dialog.
* dayOfMonth The initial day of the dialog.
* */
datePickerDialog = new DatePickerDialog(this, onDateSetListener, 2012, 11, 29);
datePickerDialog.setTitle("设置日期");
datePickerDialog.setMessage("设置闹钟日期");
datePickerDialog.setCancelable(false);
datePickerDialog.show();
}
public void fun1(){
/*
* android.app.TimePickerDialog.TimePickerDialog(Context context,
* OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)
*
* Parameters:
* context Parent.
* callBack How parent is notified.
* hourOfDay The initial hour.
* minute The initial minute.
* is24HourView Whether this is a 24 hour view, or AM/PM.
*/
timePickerDialog = new TimePickerDialog(this, onTimeSetListener, 0, 0, true);
timePickerDialog.setTitle("时间设置");
timePickerDialog.setMessage("设置闹钟时间");
/* Sets whether this dialog is cancelable with the BACK key
*
设置这个dialog是否能够被back键取消
* */
timePickerDialog.setCancelable(false);
timePickerDialog.setButton(DialogInterface.BUTTON1, "确定",timePickerDialog);
timePickerDialog.setButton(DialogInterface.BUTTON2, "退出",(android.content.DialogInterface.OnClickListener)null);
timePickerDialog.show();
}
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
fun1();
break;
case R.id.button2:
fun2();
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) this.findViewById(R.id.timePicker1);
datePicker = (android.widget.DatePicker) this.findViewById(R.id.datePicker1);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="timepickerDialog" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="datepickerDialog" />
</LinearLayout>