我想根据用户在TimePicker中的选择设置闹钟。 TimePicker设置为AM / PM模式。 为了知道用户是否希望将闹铃设置为上午10点还是下午10点,我应该如何获取AM / PM值?
侦听器 TimePickerDialog.OnTimeSetListener
仅传递小时和分钟。
我想根据用户在TimePicker中的选择设置闹钟。 TimePicker设置为AM / PM模式。 为了知道用户是否希望将闹铃设置为上午10点还是下午10点,我应该如何获取AM / PM值?
侦听器 TimePickerDialog.OnTimeSetListener
仅传递小时和分钟。
以上答案是正确的,我发现了找到上午/下午最简单的方法。
TimePickerDialog.OnTimeSetListener onStartTimeListener = new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String AM_PM ;
if(hourOfDay < 12) {
AM_PM = "AM";
} else {
AM_PM = "PM";
}
mStartTime.setText(hourOfDay + " : " + minute + " " + AM_PM );
}
};
在回调函数中
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
调用下面的函数
private String getTime(int hr,int min) {
Time tme = new Time(hr,min,0);//seconds by default set to zero
Format formatter;
formatter = new SimpleDateFormat("h:mm a");
return formatter.format(tme);
}
这些函数返回类似于“1:12 PM”的内容。
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm a",Locale.getDefault());
Calenter calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
calendar.set(Calendar.MINUTE, tp.setCurrentMinutes());
calendar.clear(Calendar.SECOND);
timeFormat.format(calendar.getTime);
- Bhavin Desai您可以使用以下方法从Timepicker获取AM / PM。 Timepicker是一个视图组,因此我们可以获取其子视图索引2,该索引表示AM / PM实际上是一个按钮。 因此,我们可以获取它的文本。
Mycallback=new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
ViewGroup vg=(ViewGroup) view.getChildAt(0);
Toast.makeText(TimePickerDemoActivity.this, hourOfDay+":"+minute+":"+((Button)vg.getChildAt(2)).getText().toString(), 5000).show();
}
};
我本想把这留作评论,但我的声望还不够。到目前为止,最简洁的获取上午/下午时间的方法是使用三元运算符:
String am_pm = (hourOfDay < 12) ? "AM" : "PM";
在这里,我从系统中获取当前日期和时间,并使用日期和时间选择器对其进行更新。
private int mMonth,mYear,mDay,mHour,mMin;
public static final int DTPKR = 1;
public static final int TMPKR = 2;
// Getting the current date and time into DatePicker dialog
public void getCurrentDate(){
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
public void getCurrentTime(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMin = c.get(Calendar.MINUTE);
}
现在创建对话框并更新值 //创建对话框
protected Dialog onCreateDialog(int id) {
switch (id) {
case DTPKR:
return new DatePickerDialog(this,lisDate, mYear, mMonth, mDay);
case TMPKR:
return new TimePickerDialog(this,lisTime,mHour, mMin, false);
}
return null;
}
//setting date and updating editText
DatePickerDialog.OnDateSetListener lisDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
etDate.setText(new StringBuilder() .append(mDay).append("/").append(mMonth+1).append("/").append(mYear));
getCurrentDate();
}
};
//setting time and updating editText
TimePickerDialog.OnTimeSetListener lisTime=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour=hourOfDay;
mMin=minute;
String AM_PM ;
if(hourOfDay < 12) {
AM_PM = "AM";
} else {
AM_PM = "PM";
mHour=mHour-12;
}
etTime.setText(mHour+":"+mMin+" "+AM_PM);
getCurrentDate();
}
};
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
String am_pm = "";
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":Integer.toString( datetime.get(Calendar.HOUR) );
((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
}
注意使用 Calendar.HOUR_OF_DAY
和 Calendar.HOUR
,如果您查看两者的文档,您就会了解这个技巧。
我认为回调函数将使用24小时制的小时数被调用,因为对话框内部使用了TimePicker
。而TimePicker
有一个getCurrentHour方法,它返回0-23格式的小时数。
例如:
TimePicker sellerTime = (TimePicker) findViewById(R.id.sellerTime);
Integer.toString(((sellerTime.getCurrentHour().intValue()) > 12) ? ((sellerTime.getCurrentHour().intValue()) - 12) :
(sellerTime.getCurrentHour().intValue())