How to set TimePicker show with format 24h

前端 未结 11 948
独厮守ぢ
独厮守ぢ 2020-12-29 18:17

I\'ve created a TimePicker in layout and I want it to show time with format 24h.

Can you help me? Thanks.

相关标签:
11条回答
  • 2020-12-29 18:38

    By default, it displays time in the AM/PM format. If you want to change time in the 24 hour format, then you can use the setIs24HourView() method. See this link: http://www.androidaspect.com/2012/06/timepicker-view-tutorial.html

    0 讨论(0)
  • 2020-12-29 18:44

    Setting TimePicker by default to 24 hour mode is not always a good idea since many countrys have diffrent convention. For this case use DateFormat.is24HourFormat(getActivity()).

    Your final code migth look like picker.setIs24HourView(DateFormat.is24HourFormat(this));.

    Further details see:

    • http://developer.android.com/guide/topics/ui/controls/pickers.html
    • http://developer.android.com/reference/android/widget/TimePicker.html
    • http://developer.android.com/reference/android/text/format/DateFormat.html
    0 讨论(0)
  • 2020-12-29 18:44

    For Time picker Dialog

    Timepicker dialog provides by android to select the time.

    Java provides default functionality by passing is24HourView == true in TimePickerDialog constructor.

    public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,boolean is24HourView) {
            this(context, 0, listener, hourOfDay, minute, is24HourView);
        }
    

    Pass true for 24 hours or pass false to set time picker dialog in AM-PM format.

    for ex: (Kotlin)

     private fun showTimePicker() {
            val cal = Calendar.getInstance()
            val dialog = TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
                Log.e("Time :","$hourOfDay:$minute")
            }, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true) //here pass true for 24hrs view else false for AM-PM(12hrs view)
            dialog.show()
        }
    
    0 讨论(0)
  • 2020-12-29 18:46

    Or you let the user decide for himself which format he want to use.

    In your MainActivty:

    DialogFragment timePicker = new TimePickerFragment();
    timePicker.show(getSupportFragmentManager(), "time picker");
    
    
       @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    
                TextView outputText= (TextView)findViewById(R.id.outputText);
                outputText.setText(hourOfDay + " : "+ minute);
    }
    

    And in a new Java Class (TimePickerFragment):

    public class TimePickerFragment extends DialogFragment {
    
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
    
    
    
        return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
    
    }}
    
    0 讨论(0)
  • 2020-12-29 18:47

    I need the same just for an application to set the duration spend for doing a task. I found that I just need to extend the TimePickerDialog class and pass True in the last constructor parameter boolean is24HourView.

    public class DurationPickerDialog extends TimePickerDialog {
    
    public DurationPickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute){
        super(context, callBack, hourOfDay, minute, true);
    }
    

    Then you only need to create this as

    TimePickerDialog durationPicker =  new DurationPickerDialog(getActivity(), this, hour, minute);
    durationPicker.setTitle("Duration:");
    
    0 讨论(0)
提交回复
热议问题