How to set TimePicker show with format 24h

前端 未结 11 947
独厮守ぢ
独厮守ぢ 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:30

    TimePicker is a view for selecting the time of day, in either 24 hour or AM/PM mode. You can use setIs24HourView(true) method with TimePicker.

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

    xml

    <TimePicker
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:id="@+id/timePicker" />
    

    java

    TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);    
    timePicker.setIs24HourView(true); 
    
    0 讨论(0)
  • 2020-12-29 18:34

    As some people have already mentioned just set the parameter to true or false.

    timePicker.setIs24HourView(true);

    You can find a complete example with source code to download from here http://www.ahotbrew.com/android-timepicker-example/

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

    You just have to retrieve the TimePicker instance from the view after inflating it, then you can modify the widget.

    Ids are arbitrary:

    View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
    TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
    timePicker.setIs24HourView(true);
    
    0 讨论(0)
  • 2020-12-29 18:37

    If you want the TimePicker to be correctly initialized with the current time in 24h format use the following:

    import java.util.Calendar;
    
    timePicker.setIs24HourView(true);
    timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
    

    Otherwise, due to Android bug, the picker will start with an incorrect hour (2 instead of 14 etc).

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

    You just need call and send true to setIs24HourView function.

    TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
    tpHourMin.setIs24HourView(true);
    
    0 讨论(0)
提交回复
热议问题