无法将 android 时间选择器模式 24 小时模式更改为 AM_PM 模式

Unable to change the android time picker mode 24hrs mode to AM_PM mode

我在这里使用 timepicker 对话框,但出于某种我不明白的原因,我无法将 time picker 的模式更改为 AM_PM mode 的模式 我试过代码 timepicker.setIs24HourView(false); 这是我的代码

onCreate(..){
button.onClickListe..(new...(){
showTimePickerDialog();
});}


 public static class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return
        return new TimePickerDialog(getActivity(), this, hour, minute,
                true);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
       view.setIs24HourView(false);//TODO not working <-------- This code is not working here
        setTimeString(hourOfDay, minute, 0);

        timeView.setText(timeString);
    }
}


`private void showTimePickerDialog() {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}`}

检查 TimePickerDialog 构造函数中的最后一个参数,它是 24 小时格式,因此通过为最后一个参数传递 false 值来禁用 24 小时格式:

return new TimePickerDialog(getActivity(), this, hour, minute,false);