/// <summary>
 /// Setups the number picker.
 /// </summary>
 private void SetupNumberPicker()
 {
     numberPicker          = FindViewById <NumberPicker>(Resource.Id.npPickersNumberPicker);
     numberPicker.MinValue = 0;
     numberPicker.MaxValue = 100;
     numberPicker.ScrollTo(0, 0);
     numberPicker.SetOnValueChangedListener(this);
 }
Esempio n. 2
0
        public TimePicker(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {
            LayoutInflater inflater =
                (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);

            inflater.Inflate(Resource.Layout.time_picker_widget, this, true);


            // hour
            mHourPicker = (NumberPicker)FindViewById(Resource.Id.hour);
            mHourPicker.SetOnValueChangedListener(new OnValueChangeListener()
            {
                OnValueChanged = (NumberPicker picker, int oldVal, int newVal) =>
                {
                    // TODO Auto-generated method stub
                    mCurrentHour = newVal;
                    if (!mIs24HourView)
                    {
                        // adjust from [1-12] to [0-11] internally, with the times
                        // written "12:xx" being the start of the half-day
                        if (mCurrentHour == 12)
                        {
                            mCurrentHour = 0;
                        }
                        if (!mIsAm)
                        {
                            // PM means 12 hours later than nominal
                            mCurrentHour += 12;
                        }
                    }
                    onTimeChanged();
                }
            });

            // digits of minute
            mMinutePicker          = (NumberPicker)FindViewById(Resource.Id.minute);
            mMinutePicker.MinValue = 0;

            mMinutePicker.MaxValue = 59;
            mMinutePicker.SetFormatter(TWO_DIGIT_FORMATTER);
            mMinutePicker.SetOnValueChangedListener(new Droid.OnValueChangeListener()
            {
                OnValueChanged = (NumberPicker spinner, int oldVal, int newVal) =>
                {
                    mCurrentMinute = newVal;
                    onTimeChanged();
                }
            });


            // digits of seconds
            mSecondPicker          = (NumberPicker)FindViewById(Resource.Id.seconds);
            mSecondPicker.MinValue = 0;

            mSecondPicker.MaxValue = 59;
            mSecondPicker.SetFormatter(TWO_DIGIT_FORMATTER);
            mSecondPicker.SetOnValueChangedListener(new Droid.OnValueChangeListener()
            {
                OnValueChanged = (NumberPicker spinner, int oldVal, int newVal) =>
                {
                    mCurrentSeconds = newVal;
                    onTimeChanged();
                }
            });

            mAmPmButton = (Button)FindViewById(Resource.Id.amPm);

            // now that the hour/minute picker objects have been initialized, set
            // the hour range properly based on the 12/24 hour display mode.
            configurePickerRanges();

            // initialize to current time
            Calendar cal = Calendar.GetInstance(ULocale.English);

            setOnTimeChangedListener(NO_OP_CHANGE_LISTENER);

            // by default we're not in 24 hour mode
            setCurrentHour(cal.Get(CalendarField.HourOfDay));

            setCurrentMinute(cal.Get(CalendarField.Minute));

            setCurrentSecond(cal.Get(CalendarField.Second));

            mIsAm = (mCurrentHour < 12);


            /* Get the localized am/pm strings and use them in the spinner */
            DateFormatSymbols dfs = new DateFormatSymbols();

            string[] dfsAmPm = dfs.GetAmPmStrings();
            mAmText = dfsAmPm[Calendar.Am];
            mPmText = dfsAmPm[Calendar.Pm];
            mAmPmButton.SetText(mIsAm ? mAmText : mPmText, TextView.BufferType.Normal);
            mAmPmButton.SetOnClickListener(new OnClickListener()
            {
                Clicked = (View v) =>
                {
                    //requestFocus();
                    if (mIsAm)
                    {
                        // Currently AM switching to PM
                        if (mCurrentHour < 12)
                        {
                            mCurrentHour += 12;
                        }
                    }
                    else
                    {
                        // Currently PM switching to AM
                        if (mCurrentHour >= 12)
                        {
                            mCurrentHour -= 12;
                        }
                    }
                    mIsAm = !mIsAm;
                    mAmPmButton.SetText(mIsAm ? mAmText : mPmText, TextView.BufferType.Normal);
                    onTimeChanged();
                }
            });

            SetEnabled();
        }