【问题标题】:Set DatePicker DialogFragment to specific date将 DatePicker DialogFragment 设置为特定日期
【发布时间】:2020-03-12 18:55:34
【问题描述】:

我有以下日期选择器对话框片段,它工作得很好,并将编辑文本中的日期设置为日期选择器中选择的日期。如果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 上看到的方法,但都没有奏效。

【问题讨论】:

    标签: c# android xamarin.android datepicker


    【解决方案1】:

    我想通了。其实很简单。要将数据传递给片段,请使用捆绑包。我更改了对片段的调用以添加包含初始日期的包,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多