将实际时间解析为 TimePicker 在活动中

问题描述

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

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

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

}

在此先感谢 Stackoverflow 上的任何人!你们真的做得很好!

干杯

解决方法

您可以在 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;
    }

在活动中

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";

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...