TimePicker – 如何获得AM或PM?

我想根据用户在TimePicker中的select来设置闹钟。 TimePicker被设置为AM / PM模式。 为了知道用户是否希望自己的闹钟设置为上午10点或晚上10点,应该如何获得AM / PM值?

侦听器TimePickerDialog.OnTimeSetListener只传递小时和分钟。

Solutions Collecting From Web of "TimePicker – 如何获得AM或PM?"

上面的答案是正确的,我find最简单的方法find上午下午。

 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 ); } }; 

您可以使用以下方法从Timepicker获取AM / PM。 Timepicker是一个viewgroup.That这就是为什么我们可以得到它的子视图索引2,它演示AM / PM实际上是一个button。 所以我们可以得到它的文本。

  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(); } }; 

在callback从

 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

我宁愿留下这个评论,但我没有足够的声誉。 到目前为止,获得AM或PM最简洁的方法是使用三元运算符:

 String am_pm = (hourOfDay < 12) ? "AM" : "PM"; 

不知道是否会帮助,但我有同样的事情。 在这里看到我的答复TimePickerDialog和AM或PM

我使用日历,这应该适用于所有版本:

 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_DAYCalendar.HOUR的使用,如果你去的文档都知道这个技巧。

我认为callback将被称为小时24格式,因为。 Dialog在内部使用TimePickerTimePicker有getCurrentHour moethod,它以0-23格式返回小时。

例如:TimePicker sellerTime =(TimePicker)findViewById(R.id.sellerTime);

Integer.toString((sellerTime.getCurrentHour()。intValue())> 12)?((sellerTime.getCurrentHour()。intValue()) – 12):( sellerTime.getCurrentHour()。intValue())

在这里,我从系统中获取当前date和时间,并使用date和时间select器对话框更新它

private int mMonth,mYear,mDay,mHour,mMin;

公共静态最终诠释DTPKR = 1;

公共静态最终诠释TMPKR = 2;

//将当前date和时间获取到DatePicker对话框中

  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); } 

现在创build对话框并更新值//创build对话框

  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(); } };