2
我想在我的活动中显示一个按钮,该按钮将当前日期显示为按钮标题,当我单击按钮以显示日期选择器对话框并显示新的选择按钮标题。如何在一个按钮中选择日期选择器+显示日期android
我该如何做到这一点?任何更好的主意?最好是用EditText做这件事,以便让用户更容易地改变日期?
我想在我的活动中显示一个按钮,该按钮将当前日期显示为按钮标题,当我单击按钮以显示日期选择器对话框并显示新的选择按钮标题。如何在一个按钮中选择日期选择器+显示日期android
我该如何做到这一点?任何更好的主意?最好是用EditText做这件事,以便让用户更容易地改变日期?
我在这里附上了代码,用于时间和日期,请根据您的要求使用此代码。 这里打算txtdate和txtTime你可以在按钮文字上设置它。
private static final int DIALOG_DATE = 1;
private static final int DIALOG_TIME = 2;
private Calendar dateTime = Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat(
"MMMM dd yyyy");
private SimpleDateFormat timeFormatter = new SimpleDateFormat(
"hh:mm a");
txtstdate.setText(dateFormatter.format(dateTime.getTime()));
txtAddtime.setText(timeFormatter.format(dateTime.getTime()));
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
dateTime.set(year, monthOfYear, dayOfMonth);
txtstdate.setText(dateFormatter.format(dateTime.getTime()));
txtAdddate.setText(dateFormatter.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
case DIALOG_TIME:
return new TimePickerDialog(this, new OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute)
{
dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateTime.set(Calendar.MINUTE, minute);
txtsttime.setText(timeFormatter.format(dateTime.getTime()));
txtAddtime.setText(timeFormatter.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.HOUR_OF_DAY),
dateTime.get(Calendar.MINUTE), false);
}
return null;
}
}
**Call Dialog:**
调用此方法按钮单击事件。
showDialog(DIALOG_DATE);
showDialog(DIALOG_TIME);
访问更多文献资料:http://hasmukhbhadani.blogspot.in/search/label/Date%20and%20time%20Dialog。 http://hasmukhbhadani.blogspot.in/search/label/DatePickerSlider%20in%20android。
按照本教程http://www.mkyong.com/android/android-date-picker-example/ –