如何在“TimePickerDialog”中获得“AM”或“PM”

时间:2015-11-19 12:05:54

标签: android datetimepicker timepickerdialog

如何获得“AM”或“PM”?我已经看到了TimePicker所具有的功能,但我没有找到任何答案。

enter image description here

  TimePickerDialog mTimePicker = new TimePickerDialog(EditEventActivity.this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker,
                                      int selectedHour, int selectedMinute) {

                    Log.i(timePicker.is24HourView()?"24":"12");
                    mTextViewTime.setText(selectedHour + ":"
                            + selectedMinute);
                    mHourOfTime = selectedHour;
                    mMinuteOfTime = selectedMinute;
                }
            }, mHourOfTime, mMinuteOfTime, is24HoursFormat);// Yes 24 hour time
    mTimePicker.show();

2 个答案:

答案 0 :(得分:1)

如果用户在设备选项中将Use 24-hour format设置为false,则会显示AM和PM。

设备上的路径:
Settings > Date-Time > Use 24-Hour-Format

来源:
我现在在我的设备上试过它。

答案 1 :(得分:0)

    scheduleTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

                    String choosedHour = "";
                    String choosedMinute = "";
                    String choosedTimeZone = "";
                    if(selectedHour > 12){
                        choosedTimeZone = "PM";
                       selectedHour = selectedHour - 12;
                        if(selectedHour < 10){
                            choosedHour = "0"+selectedHour;
                        }else{
                            choosedHour = ""+selectedHour;
                        }
                    }else{
                        choosedTimeZone = "AM";
                        if(selectedHour < 10){
                            choosedHour = "0"+selectedHour;
                        }else{
                            choosedHour = ""+selectedHour;
                        }
                    }

                    if(selectedMinute < 10){
                        choosedMinute = "0"+selectedMinute;
                    }else{
                        choosedMinute= ""+selectedMinute;
                    }

                    scheduleTime.setText(choosedHour + ":" + choosedMinute +" "+choosedTimeZone);
                    //scheduleTime.setText(selectedHour + ":" + selectedMinute);
                    //scheduleTime.setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
                }
            }, hour, minute+30, false);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();
        }
    });

由于Android只提供24小时格式,即使您将其设置为false。所以只有处理它的方法是用值解析的条件(即)如果selectedHour大于12那么它将是PM,我们还需要将它减去12,这样我们才能得到精确的12小时格式。而且我还添加了&#34; 0&#34;在小时之前,如果小时小于10,以避免4:1AM格式。