解析一个实际时间给TimePicker

Parse an actual time to TimePicker

我是新来的,所以请多多包涵。是的,我试图 google 我的问题,但我也不确定我找到的答案。 所以这是我的问题: 我想构建一个应用程序,用户可以通过单击不同的按钮 select 一个时间,然后计算它们之间的时间差。 ButtonClick 打开 TimePicker 对话框,我在 Microsoft Docs 上找到的示例代码始终使用实际时间。我想要的是使用解析 Buttontext 的最后有效时间。但我不知道如何将 senderbutton 的 ID 传递给 TimePicker class.

这是按钮的事件处理程序:

    void TimeSelectOnClick (object sender, EventArgs eventArgs)
    {
        // Instantiate a TimePickerFragment (defined below) 
        TimePickerFragment frag = TimePickerFragment.NewInstance (

            // Create and pass in a delegate that updates the Activity time display 
            // with the passed-in time value:
            delegate (DateTime time)
            {
                timeSelectButton.Text = time.ToString("HH:mm");
            });

        // Launch the TimePicker dialog fragment (defined below):
        frag.Show(FragmentManager, TimePickerFragment.TAG);
    }

这是 TimePicker 对话框片段:

// TimePicker dialog fragment
public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
    // TAG used for logging
    public static readonly string TAG = "MyTimePickerFragment";

    // Initialize handler to an empty delegate to prevent null reference exceptions:
    Action<DateTime> timeSelectedHandler = delegate { };

    // Factory method used to create a new TimePickerFragment:
    public static TimePickerFragment NewInstance(Action<DateTime> onTimeSelected)
    {
        // Instantiate a new TimePickerFragment:
        TimePickerFragment frag = new TimePickerFragment();

        // Set its event handler to the passed-in delegate:
        frag.timeSelectedHandler = onTimeSelected;

        // Return the new TimePickerFragment:
        return frag;
    }

    // Create and return a TimePickerDemo:
    public override Dialog OnCreateDialog (Bundle savedInstanceState)
    {
        //Set the TimePicker default time 
        //Here i Want to parse the time from the button something like DateTime.Parse(buttonID.Text);
        //Either with current time or parsed time... how to pass values from the sender button i have no idea
        DateTime currentTime = DateTime.Parse("06:00");

        // force 24-hour time format:
        bool is24HourFormat = true;

        // Instantiate a new TimePickerDemo, passing in the handler, the current 
        // time to display, and whether or not to use 24 hour format:
        TimePickerDialog dialog = new TimePickerDialog
            (Activity, this, currentTime.Hour, currentTime.Minute, is24HourFormat);
   
        // Return the created TimePickerDemo:
        return dialog;
    }

    // Called when the user sets the time in the TimePicker: 
    public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        // Get the current time:
        DateTime currentTime = DateTime.Now;

        // Create a DateTime that contains today's date and the time selected by the user:
        DateTime selectedTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hourOfDay, minute, 0);

        // Log the date and selected time:
        Log.Debug(TAG, selectedTime.ToLongTimeString());

        // Invoke the handler to update the Activity's time display to the selected time:
        timeSelectedHandler (selectedTime);
    }

}

提前感谢 Whosebug 上的任何人!你们真的做得很好!

干杯

您可以在 TimePickerFragment

中添加一个 属性
static string TimeString;
 public static TimePickerFragment NewInstance(Action<DateTime> onTimeSelected,string time)
    {
        // Instantiate a new TimePickerFragment:
        TimePickerFragment frag = new TimePickerFragment();

        // Set its event handler to the passed-in delegate:
        frag.timeSelectedHandler = onTimeSelected;

        TimeString = time;

        // Return the new TimePickerFragment:
        return frag;
    }

在Activity

TimePickerFragment frag = TimePickerFragment.NewInstance (

            // Create and pass in a delegate that updates the Activity time display 
            // with the passed-in time value:
            delegate (DateTime time)
            {
                timeSelectButton.Text = time.ToString("HH:mm");
            },buttonID.Text);

并且可以任意修改

TimePickerFragment.TimeString = "xxx";