时间和日期选择器 android
Time and date picker android
我正在开发 android 应用程序,我想在其中 select 日期和时间。我正在使用以下方法来获得对话时间。但我想做类似的事情,当我点击时间对话框时,它也会显示日期。请帮帮我:
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
}
};
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
txtTimePicker.setText(aTime);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
如果您同时需要时间选择器和日期选择器,则需要使用两个不同的对话框 类:TimePickerDialog and DatePickerDialog.
因此,如果您也希望用户选择日期,则需要在显示时间选择器一之前或之后显示一个 DatePickerDialog。您可以像显示 TimePickerDialog 一样显示 DatePickerDialog。
根据您的要求输入以下代码即可:
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
dateObj = curFormater.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
txtTimePicker.setText(newDateStr+" "+aTime);
我正在开发 android 应用程序,我想在其中 select 日期和时间。我正在使用以下方法来获得对话时间。但我想做类似的事情,当我点击时间对话框时,它也会显示日期。请帮帮我:
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
}
};
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
txtTimePicker.setText(aTime);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
如果您同时需要时间选择器和日期选择器,则需要使用两个不同的对话框 类:TimePickerDialog and DatePickerDialog.
因此,如果您也希望用户选择日期,则需要在显示时间选择器一之前或之后显示一个 DatePickerDialog。您可以像显示 TimePickerDialog 一样显示 DatePickerDialog。
根据您的要求输入以下代码即可:
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
dateObj = curFormater.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
txtTimePicker.setText(newDateStr+" "+aTime);