Xamarin TimePicker 片段对话框

Xamarin TimePicker Fragment Dialog

我正在使用 C# 开发 Xamarin Android 应用程序,我正在尝试实现一个 TimePicker 对话框,用户可以在其中选择给定时间,他们每天都会收到通知(或他选择的一周中的几天)。

我知道有一个 TimePickerDialog 控件已被弃用,我们现在需要使用片段对话框。如果有人能帮助我,我将不胜感激。

谢谢!

您最好的选择是扩展 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
    }
}

这是项目中的示例代码:

class TimePickerFragment : DialogFragment
{
    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        return new TimePickerDialog(Activity, (sender, args) =>
        {
            //args contains new time
        }, DateTime.Now.Hour, DateTime.Now.Minute, true);
    }
}

这是我如何实现 TimePickerDialog 的示例

  1. 声明 EditText 控件(您可以只使用 TextView)

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

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

    //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);
    }
    

希望对您有所帮助!