Android时间选择器显示两个时钟?

时间:2016-08-26 17:42:54

标签: android timepicker

我根据android dev doc实现了Android时间选择器,一切正常,但我有一个奇怪的错误。我搜索了很多,没有找到任何东西,我在这里问。

这是我的时间选择器:
timepicker picture - to see the bug

以下是代码:

 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 it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // Do something with the time chosen by the user
            if (hourOfDay < 10) {
                startTime.setText("0" + Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
            } else if (minute < 10) {
                startTime.setText(Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
            } else if (hourOfDay < 10 && minute < 10) {
                startTime.setText("0" + Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
            } else if (minute == 0) {
                startTime.setText(Integer.toString(hourOfDay) + ":00");
            } else
                startTime.setText(Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
        }
    }

有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

TimePickerDialog (Context context, 
            int themeResId, 
            TimePickerDialog.OnTimeSetListener listener, 
            int hourOfDay, 
            int minute, 
            boolean is24HourView)

is24HourView :这是24小时视图还是上午/下午。

尝试将此参数设置为 false ,我想你会得到你想要的。

参考:https://developer.android.com/reference/android/app/TimePickerDialog.html

答案 1 :(得分:0)

我在三星设备(我的情况下为Galaxy S5和S6)上遇到了同样的问题。我发现在此设备上标题是由系统添加的,要隐藏该标题,您所需要做的就是自行设置为null。 Hierarchy view of broken TimePickerDialog in my case

TimePickerDialog dialog = new TimePickerDialog(getActivity(), this, hour, minute, false);
dialog.setTitle(null);