0

I'm trying to set the TimePickerDialog to intervals of 10 mins, I have seen various solutions that look like they should work however the most elegant solution seems to be to derive from TimePickerDialog and then override the NumberPicker properties to 00, 10, 20 ... 40 rather than 00, 01, 02 ... 59.

The solution in Java seems to be in the constructor of you derived class grab the NumberPicker control and reset all it's values to show what you want. The solution can be found here by 01.sunlit Set TimePickerDialog to use 15 minute intervals

I have attempted to implement this solution in Xamarin but I can't get access to the number picker object.

Can you tell me how to access this object within the constructor of my derived class?

4

1 回答 1

3

所以我设法让它像这样工作:

public class TimePickerDialogIntervals : TimePickerDialog
{
    public const int TimePickerInterval = 10;
    private bool _ignoreEvent = false;

    public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
            : base(context, (sender, e) => {
                callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
            }, hourOfDay, minute/TimePickerInterval, is24HourView)
    {
    }

    protected TimePickerDialogIntervals(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void SetView(View view)
    {
        SetupMinutePicker (view);
        base.SetView(view);
    }

    void SetupMinutePicker (View view)
    {
        var numberPicker = FindMinuteNumberPicker (view as ViewGroup);
        if (numberPicker != null) {
            numberPicker.MinValue = 0;
            numberPicker.MaxValue = 5;
            numberPicker.SetDisplayedValues (new String[] { "00", "10", "20", "30", "40", "50" });
        }
    }

    protected override void OnCreate (Android.OS.Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        GetButton((int)DialogButtonType.Negative).Visibility = Android.Views.ViewStates.Gone;
        this.SetCanceledOnTouchOutside (false);

    }

    private NumberPicker FindMinuteNumberPicker(ViewGroup viewGroup)
    {
        for (var i = 0; i < viewGroup.ChildCount; i++)
        {
            var child = viewGroup.GetChildAt(i);
            var numberPicker = child as NumberPicker;
            if (numberPicker != null)
            {
                if (numberPicker.MaxValue == 59)
                {
                    return numberPicker;
                }
            }

            var childViewGroup = child as ViewGroup;
            if (childViewGroup != null)
            {
                var childResult = FindMinuteNumberPicker (childViewGroup);
                if(childResult !=null)
                    return childResult;
            }
        }

        return null;
    }
}
于 2014-01-20T13:11:08.550 回答