Xamarin TimePicker片段对话框

时间:2016-04-13 13:22:11

标签: c# android android-fragments xamarin android-timepicker

我正在使用C#中的Xamarin Android应用程序,我正在尝试实现一个TimePicker对话框,用户在其中选择给定时间,并且每天都会通知他们(或者一周中的那些日子)他选择了。)

我理解有一个不推荐使用的TimePickerDialog控件,我们现在需要使用Fragment Dialog。如果有人可以帮助我,我会非常感激。

谢谢!

2 个答案:

答案 0 :(得分:3)

您最好的选择是延长DialogFragment并从中创建自己的TimePickerFragment。 Android甚至还有关于该主题的完整文档:

http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker

注意:你必须转换相应的Java - > C#,但这不应该是一个太大的问题)

public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        Calendar c = Calendar.Instance;
        int hour = c.Get(CalendarField.HourOfDay);
        int minute = c.Get(CalendarField.Minute);
        return new TimePickerDialog(Activity, this, hour, minute, DateFormat.Is24HourFormat(Activity));
    }

    public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        //Do something when time chosen by user
    }
}

答案 1 :(得分:1)

以下是我如何实现TimePickerDialog

的示例
  1. 声明EditText控件(您可以使用TextView)

    EditText startTimeInput = FindViewById(Resource.Id.newEventStartTimeInput);

  2. 通过扩展DialogFragment并处理OnCreate和OnTimeSet方法来创建TimePickerFragment

    //Time Picker
    public class TimePickerFragment : DialogFragment, 
                              TimePickerDialog.IOnTimeSetListener
    {
        // TAG can be any string of your choice.
        public static readonly string TAG = "Y:" + typeof(TimePickerFragment).Name.ToUpper();
    
        // Initialize this value to prevent NullReferenceExceptions.
        Action<TimeSpan> _timeSelectedHandler = delegate { };
    
        public static TimePickerFragment NewInstance(Action<TimeSpan> onTimeSet)
        {
            TimePickerFragment frag = new TimePickerFragment();
            frag._timeSelectedHandler = onTimeSet;
            return frag;
        }
    
        public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            Calendar c = Calendar.Instance;
            int hour = c.Get(CalendarField.HourOfDay);
            int minute = c.Get(CalendarField.Minute);
            bool is24HourView = true;
            TimePickerDialog dialog = new TimePickerDialog(Activity,
                                                           this,
                                                           hour,
                                                           minute,
                                                           is24HourView);
            return dialog;
        }
    
        public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
        {
            //Do something when time chosen by user
            TimeSpan selectedTime = new TimeSpan(hourOfDay, minute, 00);
            Log.Debug(TAG, selectedTime.ToString());
            _timeSelectedHandler(selectedTime);
        }
    }
    
  3. 显示片段

        private void StartTimeInput_Click(object sender, EventArgs e)
    {
        TimePickerFragment frag = TimePickerFragment.NewInstance(delegate (TimeSpan time)
        {
            startTimeInput.Text = time.ToString();
        });
        frag.Show(FragmentManager, TimePickerFragment.TAG);
    }
    
  4. 希望这有帮助!