Android 如何将EditText的值设置为下午1:00或上午8:00?
我正在使用此代码从时间选择器获取时间Android 如何将EditText的值设置为下午1:00或上午8:00?,android,datetime,android-layout,android-ndk,timepicker,Android,Datetime,Android Layout,Android Ndk,Timepicker,我正在使用此代码从时间选择器获取时间 @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this,mTimeSetListener, Calendar.HOUR, Calendar.MINUTE, false); } return null; } pri
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,mTimeSetListener, Calendar.HOUR, Calendar.MINUTE, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
reminderSetTime.setText(""+hourOfDay+":"+minute+"");
Toast.makeText(ImportantDateReminderActivity.this, "Time is="+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
};
static final int TIME_DIALOG_ID = 0;
要显示时间选择器,我使用以下代码:
case R.id.reminderTime:
showDialog(TIME_DIALOG_ID);
break;
这样做,我没有得到适当的时间价值。
我想要像下午1:00和上午10:00这样的值
但我没有得到下午/上午,如果时间超过12,那么我得到13,14。比如数字,我想要1到12的数字
那么如何设置该值??
谢谢
这对我有用
用这个
private void updateTimeDisplay() {
try {
Format formatter;
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
Date d = df.parse(mHour + ":" + mMinute);
Calendar gc = new GregorianCalendar();
gc.setTime(d);
gc.add(Calendar.HOUR, 0);
Date d2 = gc.getTime();
formatter = new SimpleDateFormat("hh:mm a");
String time = formatter.format(d2);
btnFromTime.setText(time);
} catch (Exception e) {
e.printStackTrace();
}
}
这对我有用
用这个
private void updateTimeDisplay() {
try {
Format formatter;
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
Date d = df.parse(mHour + ":" + mMinute);
Calendar gc = new GregorianCalendar();
gc.setTime(d);
gc.add(Calendar.HOUR, 0);
Date d2 = gc.getTime();
formatter = new SimpleDateFormat("hh:mm a");
String time = formatter.format(d2);
btnFromTime.setText(time);
} catch (Exception e) {
e.printStackTrace();
}
}
你试过了吗:
TimePicker.setIs24HourView(false)
?你试过了吗:TimePicker.setIs24HourView(false)
?机器人的最佳实践是用户想做什么就做什么;Android设置中设置了什么:
检查时间_12 _24
回答您的问题,您可以使用DateFormat类来实现您想要的Androids的最佳实践是用户想做什么就做什么;Android设置中设置了什么: 检查时间_12 _24
回答您的问题,您可以使用DateFormat类来获得您想要的内容这很奇怪,因为is24HourView为false。有日志吗?这很奇怪,因为你的IS24小时视图是假的。有日志吗?什么是“pad”beforemin对象?公共字符串pad(intc){if(c>=10)返回String.valueOf(c);否则返回“0”+String.valueOf(c);}什么是“pad”beforemin对象?公共字符串填充(int c){if(c>=10)返回String.valueOf(c);否则返回“0”+String.valueOf(c);}