我有以下“日期选择器”对话框片段,该片段工作正常,并将edittext中的日期设置为在日期选择器中选择的日期。如果edittext不为空,我需要将日期选择器中的日期设置为edittext中的日期。我的对话框片段是:
public class DatePickerFragment : Android.Support.V4.App.DialogFragment, DatePickerDialog.IOnDateSetListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();
// Initialize this value to prevent NullReferenceExceptions.
Action<DateTime> _dateSelectedHandler = delegate { };
public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
{
DatePickerFragment frag = new DatePickerFragment();
frag._dateSelectedHandler = onDateSelected;
return frag;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currently = DateTime.Now;
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
currently.Year,
currently.Month - 1,
currently.Day);
return dialog;
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Note: monthOfYear is a value between 0 and 11, not 1 and 12!
DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
Log.Debug(TAG, selectedDate.ToLongDateString());
_dateSelectedHandler(selectedDate);
}
}
单击编辑文本后我将该片段称为,该代码为:
dob.Click += (sender, e) =>
{
DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
{
string thedate = time.ToShortDateString();
if (Utils.Mid(thedate,2,1) == "/")
{
thedate = "0" + thedate;
}
dob.Text = thedate;
});
frag.Show(SupportFragmentManager, DatePickerFragment.TAG);
};
我已经尝试了几种我在stackoverflow上看到的方法,但至今没有任何效果。
我知道了。其实很简单。要将数据传递到片段,请使用包。我将对片段的调用更改为添加包含初始日期的包,如下所示:
dob.Click += (sender, e) =>
{
Bundle bundlee = new Bundle();
bundlee.PutString("initDate", dob.Text);
DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
{
string thedate = time.ToShortDateString();
if (Utils.Mid(thedate,2,1) == "/")
{
thedate = "0" + thedate;
}
dob.Text = thedate;
});
frag.Arguments = bundlee;
frag.Show(SupportFragmentManager, DatePickerFragment.TAG);
};
然后在片段的OnCreateDialog
方法中,我从包中获取数据并设置日期选择器的初始日期,如下所示:
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
Bundle bundlee = this.Arguments;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
int year = DateTime.Now.Year;
string initDate = bundlee.GetString("initDate");
if (initDate != "" && initDate != null)
{
month = Convert.ToInt32(Utils.Mid(initDate, 1, 2));
day = Convert.ToInt32(Utils.Mid(initDate, 4, 2));
year = Convert.ToInt32(Utils.Mid(initDate, 7, 4));
}
//DateTime currently = DateTime.Now;
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
year,
month - 1,
day);
return dialog;
}
[作为以VB4开头的终身VB程序员,我无法没有Mid
函数,因此我在c#中创建了一个等效项并将其放在我的Utils
类中(以防万一您想知道它在哪里出现)来自):
class Utils
{
public static string Mid(string input, int index, char newChar)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
char[] chars = input.ToCharArray();
chars[index - 1] = newChar;
return new string(chars);
}
public static string Mid(string s, int a, int b)
{
string temp = s.Substring(a - 1, b);
return temp;
}
}