我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。


当前回答

我对沙玛林的解决方案。基于Linh的MvvmCross的Android:

public class DatePickerEditText : EditText, DatePickerDialog.IOnDateSetListener
{
    IDisposable _clickSubscription;

    public override bool Clickable => true;

    protected DatePickerEditText(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
        => Init();

    public DatePickerEditText(Context context) : base(context)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs) 
        : base(context, attrs)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr) 
        : base(context, attrs, defStyleAttr)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) 
        : base(context, attrs, defStyleAttr, defStyleRes)
        => Init();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _clickSubscription?.Dispose();
            _clickSubscription = null;
        }

        base.Dispose(disposing);
    }

    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
        => Text = view.DateTime.ToString("d", CultureInfo.CurrentUICulture);

    void Init()
    {
        SetFocusable(ViewFocusability.NotFocusable);
        _clickSubscription = this.WeakSubscribe(nameof(Click), OnClick);
    }

    void OnClick(object sender, EventArgs e)
    {
        var date = DateTime.Today;
        try
        {
            date = DateTime.Parse(Text, CultureInfo.CurrentUICulture);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex);
        }

        var dialog = new DatePickerDialog(Context,
                                          this,
                                          date.Year,
                                          date.Month,
                                          date.Day);
        dialog.Show();
    }
}

来杯香草沙玛林。Android版本只需将WeakSubscribe替换为EditText的Click事件的常规订阅,不要忘记在Dispose方法覆盖中取消订阅。

其他回答

把这4行放在(EditText, AppCompatEditText)上。它不会显示键盘。然后在clicklistener上,你可以显示datePicker

android:clickable="false" 
   android:cursorVisible="false" 
   android:focusable="false" 
   android:focusableInTouchMode="false"

使用片段、MvvmCross和Xamarin的解决方案。安卓

public class EnterTimeView : MvxFragment, DatePickerDialog.IOnDateSetListener
    {            
        private EditText datePickerText;
        public EnterTimeView()
        {
            this.RetainInstance = true;
        }

        public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
        {
            this.HasOptionsMenu = true;

            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            var view = inflater.Inflate(Resource.Layout.EnterTimeView, container, false);

            datePickerText = view.FindViewById<EditText>(Resource.Id.DatePickerEditText);
            datePickerText.Focusable = false;
            datePickerText.Click += delegate
            {
                var dialog = new DatePickerDialogFragment(Activity, Convert.ToDateTime(datePickerText.Text), this);
                dialog.Show(FragmentManager, "date");
            };

            var set = this.CreateBindingSet<EnterTimeView, EnterTimeViewModel>();    
            set.Bind(datePickerText).To(vm => vm.Date);
            set.Apply();

            return view;
        }   

        public void OnDateSet(Android.Widget.DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            datePickerText.Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString();
        }

        private class DatePickerDialogFragment : Android.Support.V4.App.DialogFragment 
        {
            private readonly Context _context;
            private DateTime _date;
            private readonly DatePickerDialog.IOnDateSetListener _listener;

            public DatePickerDialogFragment(Context context, DateTime date, DatePickerDialog.IOnDateSetListener listener)
            {
                _context = context;
                _date = date;
                _listener = listener;
            }

            public override Dialog OnCreateDialog(Bundle savedState)
            {
                var dialog = new DatePickerDialog(_context, _listener, _date.Year, _date.Month - 1, _date.Day);
                return dialog;
            }
        }

我认为最好重写EditText类…

public class EditTextDP extends android.support.v7.widget.AppCompatEditText implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
    public EditTextDP(Context context) {
        super(context);
        setOnClickListener(this);
        setFocusable(false);
    }

    public EditTextDP(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        setFocusable(false);
    }

    public EditTextDP(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnClickListener(this);
        setFocusable(false);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        setText(new StringBuilder().append(dayOfMonth).append("/").append(monthOfYear + 1).append("/").append(year));
    }

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        DatePickerDialog dialog = new DatePickerDialog(getContext(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
}

然后在XML中使用它…

            <*.*.*.EditTextDP
                android:id="@+id/c1_dob_hm"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edittext_white"
                android:ems="10"
                android:padding="8dp">

            </*.*.*.EditTextDP>

我对沙玛林的解决方案。基于Linh的MvvmCross的Android:

public class DatePickerEditText : EditText, DatePickerDialog.IOnDateSetListener
{
    IDisposable _clickSubscription;

    public override bool Clickable => true;

    protected DatePickerEditText(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
        => Init();

    public DatePickerEditText(Context context) : base(context)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs) 
        : base(context, attrs)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr) 
        : base(context, attrs, defStyleAttr)
        => Init();

    public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) 
        : base(context, attrs, defStyleAttr, defStyleRes)
        => Init();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _clickSubscription?.Dispose();
            _clickSubscription = null;
        }

        base.Dispose(disposing);
    }

    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
        => Text = view.DateTime.ToString("d", CultureInfo.CurrentUICulture);

    void Init()
    {
        SetFocusable(ViewFocusability.NotFocusable);
        _clickSubscription = this.WeakSubscribe(nameof(Click), OnClick);
    }

    void OnClick(object sender, EventArgs e)
    {
        var date = DateTime.Today;
        try
        {
            date = DateTime.Parse(Text, CultureInfo.CurrentUICulture);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex);
        }

        var dialog = new DatePickerDialog(Context,
                                          this,
                                          date.Year,
                                          date.Month,
                                          date.Day);
        dialog.Show();
    }
}

来杯香草沙玛林。Android版本只需将WeakSubscribe替换为EditText的Click事件的常规订阅,不要忘记在Dispose方法覆盖中取消订阅。

 selectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog  mdiDialog =new DatePickerDialog(mContext,new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Toast.makeText(getApplicationContext(),year+ " "+monthOfYear+" "+dayOfMonth,Toast.LENGTH_LONG).show();


                    }
                }, year, month, date);
                mdiDialog.show();

            }

        });