如何在Android中设置自定义分钟间隔TimePickerDialog自定义、间隔、如何在、TimePickerDialog

2023-09-04 05:49:33 作者:傲气稳了全场

我有一个TimePickerDialog工作设置被设定为一个TextView,以显示它的时间。现在,我需要帮助到TimePicker minutues间隔(TimePickerDialog内部)设置为15分钟。我已经看到有超过15分钟的时间间隔问题涉及到TimePicker一个职位,但我不知道如何将它应用到TimePickerDialog,因为我不知道如何使用TimePicker它是TimePickerDialog内创建。我是新来的Andr​​oid和在这个问题上完全丧失。先谢谢了。

I have got a TimePickerDialog working to set time which is set to a TextView in order to display it. Now, I need help to set that TimePicker (inside the TimePickerDialog) minutues interval to 15 minutes. I have seen there is a post with 15 minutes interval issue related to TimePicker, but I don't know how to apply it to the TimePickerDialog because I don't know how to use the TimePicker that it is created inside the TimePickerDialog. I am new to Android and completely lost in this matter. Thanks in advance.

推荐答案

使用这从@Rizwan和 的其他线程,我想出了一个综合的解决方案,允许任意分钟为增量在 TimePickerDialog 。主要的问题是,大部分的功能​​都​​隐藏在Android TimePickerDialog TimePicker 类和它不会出现为

Using a combination of this from @Rizwan and this other thread, I came up with a combined solution that allows arbitrary minute increments in a TimePickerDialog. The main issue is that most of the functionality is hidden by the android TimePickerDialog and TimePicker classes and it doesn't appear to be

扩展 TimePickerDialog 来让我们更容易获得 使用反射来达到显示的内部和访问所需的位(见下文) 重新布线分钟NumberPicker,以显示我们的价值观 重新布线的 TimePicker 来接收和返回值构成的 NumberPicker 履行我们的定制增量。 块的的onStop()的,这样它不会重置接近的值。 Extend TimePickerDialog to allow us easier access Use reflection to reach inside the display and access the required bits (see below) rewire the minute 'NumberPicker' to display our values rewire the TimePicker to receive and return values form the NumberPicker honoring our custom increment. block onStop() so that it doesn't reset the value on close.

与到达的用户界面内的主要问题在于,元件由IDS其中有可能改变所引用,而id的连名字不能保证永远是相同的。话虽如此,这是工作,稳定的解决方案,并有可能继续为实现可预见的未来。在我看来,空的catch块应该警告用户界面发生了变化,应该回落到默认值(增量1分钟)的行为。

Warning

The main issue with reaching inside the UI is that elements are referenced by ids which are likely to change, and even the name of the id is not guaranteed to be the same forever. Having said that, this is working, stable solution and likely to work for the foreseeable future. In my opinion the empty catch block should warn that the UI has changed and should fall back to the default (increment 1 minute) behaviour.

    private class DurationTimePickDialog extends TimePickerDialog
    {
        final OnTimeSetListener mCallback;
        TimePicker mTimePicker;
        final int increment;

        public DurationTimePickDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, int increment)
        {
            super(context, callBack, hourOfDay, minute/increment, is24HourView);
            this.mCallback = callBack;
            this.increment = increment;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
                if (mCallback != null && mTimePicker!=null) {
                    mTimePicker.clearFocus();
                    mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
                            mTimePicker.getCurrentMinute()*increment);
                }
        }

        @Override
        protected void onStop()
        {
            // override and do nothing
        }

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            try
            {
                Class<?> rClass = Class.forName("com.android.internal.R$id");
                Field timePicker = rClass.getField("timePicker");
                this.mTimePicker = (TimePicker)findViewById(timePicker.getInt(null));
                Field m = rClass.getField("minute");

                NumberPicker mMinuteSpinner = (NumberPicker)mTimePicker.findViewById(m.getInt(null));
                mMinuteSpinner.setMinValue(0);
                mMinuteSpinner.setMaxValue((60/increment)-1);
                List<String> displayedValues = new ArrayList<String>();
                for(int i=0;i<60;i+=increment)
                {
                    displayedValues.add(String.format("%02d", i));
                }
                mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}

构造接受增量值和保留了一些其他的参考。请注意,这忽略了错误检查,我们会preFER 60%的增量== 0

constructor accepts the increment value and retains some other references. Note that this omits error checking and we'd prefer 60%increment==0

的onCreate 使用的用户界面领域和反思的名字,发现当前的位置。同样,这忽略了错误检查和应该是故障安全,即恢复到默认行为,如果出现错误。

onCreate uses the name of the UI fields and reflection to discover the current location. Again this omits error checking and should be 'fail-safe' ie revert to default behaviour if something goes wrong.

的onClick 覆盖到正确的分钟值返回给回调监听

onClick overridden to return the correct minute value to the callback listener

的onStop 覆盖,以prevent返回第二次,在对话框关闭时(不正确)的指标值。来吧,自己尝试一下。

onStop overridden to prevent the (incorrect) index value being returned a second time, when the dialog closes. Go on, try it yourself.

这其中大部分来自于挖掘到TimePickerDialog源。

Most of this comes from digging into the TimePickerDialog source.