如何分别更新多个时间选择器

时间:2016-03-17 15:25:30

标签: c# android xamarin xamarin.android timepicker

过去几天我一直在试验Xamarin.Android中的时间选择器。我跟着这两个链接:

How to update text-view with multiple datepickers

http://blog.falafel.com/31-days-of-xamarin-android-day-15-controls-datetime-controls/

但我现在有点卡住了。

我在同一个活动中有两个时间选择器。我想选择开始时间和结束时间。但是当我在活动中设置其中一个时间选择器的时间时,它会自动更新另一个。

我想要的是设置一次选择器而不更新另一个。 以下是我的尝试:

ReservationHoursActivity.cs

public class ReservationHoursActivity : Activity, TimePickerDialog.IOnTimeSetListener
{
    private TextView time_display_Start;
    private Button pick_button_Start;
    private TextView time_display_End;
    private Button pick_button_End, submitButtonMyslot;

    private int hour;
    private int minute;

    const int START_TIME_DIALOG_ID = 0;
    const int END_TIME_DIALOG_ID = 1;

    const int TIME_PICKER_TO = 0;
    const int TIME_PICKER_FROM = 1;

    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        SetContentView (Resource.Layout.ReservationHoursLayout);

        time_display_Start = FindViewById<TextView> (Resource.Id.timeDisplayStart);
        pick_button_Start = FindViewById<Button> (Resource.Id.btnPickTimeStart);

        time_display_End = FindViewById<TextView> (Resource.Id.timeDisplayEnd);
        pick_button_End = FindViewById<Button> (Resource.Id.btnPickTimeEnd);

        submitButtonMyslot = FindViewById<Button> (Resource.Id.btnSubmitSlot);

        submitButtonMyslot.Click += SubmitButtonMyslot_Click;

        pick_button_Start.Click += delegate {
            ShowTimePickerDialog (START_TIME_DIALOG_ID);
        };

        pick_button_End.Click += delegate {
            ShowTimePickerDialog (END_TIME_DIALOG_ID);
        };

        hour = DateTime.Now.Hour;
        minute = DateTime.Now.Minute;

        UpdateDisplayStart (hour, minute);
        UpdateDisplayEnd (hour, minute);
    }


    void ShowTimePickerDialog (int pickerID)
    {
        switch (pickerID) {

        case TIME_PICKER_FROM:
            var dialogStart = new TimePickerFragment (this, hour, minute, this);
            dialogStart.Show (FragmentManager, null);
            break;

        case TIME_PICKER_TO:
            var dialogEnd = new TimePickerFragment (this, hour, minute, this);
            dialogEnd.Show (FragmentManager, null);
            break;
        }

    }

    public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
    {
        UpdateDisplayEnd (hourOfDay, minute);
        UpdateDisplayStart (hourOfDay, minute);
    }

    void UpdateDisplayStart (int selectedHours, int selectedMinutes)
    {
        time_display_Start.Text = selectedHours + ":" + selectedMinutes;
    }

    void UpdateDisplayEnd (int selectedHours, int selectedMinutes)
    {
        time_display_End.Text = selectedHours + ":" + selectedMinutes;
    }
}

TimePickerFragment.cs

public class TimePickerFragment:DialogFragment
{
    private readonly Context context;
    private  int hour;
    private int minute;
    private readonly TimePickerDialog.IOnTimeSetListener listener;

    public TimePickerFragment (Context context, int hour, int minute, TimePickerDialog.IOnTimeSetListener listener)
    {
        this.context = context;
        this.hour = hour;
        this.minute = minute;
        this.listener = listener;
    }

    public override Dialog OnCreateDialog (Bundle savedState)
    {
        var dialog = new TimePickerDialog (context, listener, hour, minute, false);
        return dialog;
    }
}

我不知道如何分别更新两个时间选择器。 我也尝试将OnTimeSet方法放在其中:

private TimePickerDialog.IOnTimeSetListener From_Time_Listener = new TimePickerDialog.IOnTimeSetListener()
    {
        public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
        {
            UpdateDisplayEnd (hourOfDay, minute);
        }
    };

但这不起作用。它不允许我把这个方法放在里面。此外,如果我这样做,其他变量和方法将是意外的。

2 个答案:

答案 0 :(得分:1)

有多种方法可以解决这个问题。最简单的方法是设置一个类级别变量来跟踪哪个选择器处于活动状态,然后使用该值来确定选择器完成时要更新的字段:

int activePickerID;

void ShowTimePickerDialog (int pickerID)
{
    activePickerID = pickerID;

    switch (pickerID) {

    case TIME_PICKER_FROM:
        var dialogStart = new TimePickerFragment (this, hour, minute, this);
        dialogStart.Show (FragmentManager, null);
        break;

    case TIME_PICKER_TO:
        var dialogEnd = new TimePickerFragment (this, hour, minute, this);
        dialogEnd.Show (FragmentManager, null);
        break;
    }

}

public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
{
    if (activePickerID == TIME_PICKER_TO) {
      UpdateDisplayEnd (hourOfDay, minute);
    } else {
      UpdateDisplayStart (hourOfDay, minute);
    }
}

答案 1 :(得分:1)

    public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
{   
if(pickerID == TIME_PICKER_FROM){
    UpdateDisplayEnd (hourOfDay, minute);
}
else if(pickerID == TIME_PICKER_TO)
{
    UpdateDisplayStart (hourOfDay, minute);
}
}