Android timepicker settext 12小时制

时间:2018-01-25 10:22:39

标签: android

我需要设置12小时时钟的文本。现在它显示12小时时钟,但在选择时间后,它将文本设置为24小时时钟

schedule_time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View 
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(Add_schedule.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    schedule_time.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, false);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();
        }
    });

1 个答案:

答案 0 :(得分:2)

你只是这样做..

        @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {

                    boolean isPM = (hourOfDay >= 12);

                    schedule_time.setText(String.format("%02d:%02d %s", (hourOfDay == 12 || hourOfDay == 0) ? 12 : hourOfDay % 12, minute, isPM ? "PM" : "AM"));


                    //button.setText( selectedHour + ":" + selectedMinute);
                }