DatePickerDialog、TimePickerDialog
==>
DatePickerDialog、TimePickerDialog功能、用法都比较简单,操作步骤:
1.通过new关键字创建DatePickerDialog、TimePickerDialog实例,调用它的show()方法即可将日期选择对话框、时间选择对话框显示出来;
2.通过为DatePickerDialog、TimePickerDialog绑定监听器,来获取用户设置的事件...
实例:
布局文件==》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/edtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btnDatePickerDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="DatepickerDialog" /> <Button
android:id="@+id/btnTimePickerDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TimePickerDialog" /> </LinearLayout> 代码实现==》
package com.example.mydatepickerdialog; import java.util.Calendar; import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker; public class MainActivity extends Activity
{
EditText EditContent; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button datepicker = (Button) this.findViewById(R.id.btnDatePickerDialog);
Button timepicker = (Button) this.findViewById(R.id.btnTimePickerDialog);
EditContent = (EditText) findViewById(R.id.edtContent); datepicker.setOnClickListener(new ButtonOnClickLinstener());
timepicker.setOnClickListener(new ButtonOnClickLinstener());
} private class ButtonOnClickLinstener implements OnClickListener
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnDatePickerDialog:
Calendar calendar = Calendar.getInstance();
// 创建DatePickerDialog对话框并显示
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
StringBuffer str = new StringBuffer();
str.append("你选择了:");
str.append(year);
str.append("年");
str.append(monthOfYear);
str.append("月");
str.append(dayOfMonth);
str.append("日"); EditContent.setText(str);
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)).show();
break; case R.id.btnTimePickerDialog:
Calendar c = Calendar.getInstance();
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
StringBuffer str = new StringBuffer();
str.append("你选择了:");
str.append(hourOfDay);
str.append("时");
str.append(minute);
str.append("分"); EditContent.setText(str);
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();// true标识采用24小时制
break;
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
运行效果:
注意:日期选择对话框、时间选择对话框只是供用户来选择日期、时间的,对于android系统的日期、时间没有影响。
android暂时还没有公开设置系统日期、时间的API,如果需要在程序中设置android系统日期、时间,目前的方式都需要重新编译android系统源代码,比较烦琐...
在使用日期选择对话框、时间选择对话框时,注意调用其show()方法,否则将不能正常显示;