选择更改Xamarin android的日期

时间:2014-08-16 16:30:38

标签: c# android xamarin

我试图在对话框中显示日历并让用户选择日期。我能够获得当前日期,但在更改日期时难以选择日期。有人可以帮我解决这个问题吗?我发布下面的代码

namespace PlayProject
{
    [Activity(Label = "GChartActivity")]
    public class GChartActivity : Activity, CalendarView.IOnDateChangeListener
    {

        private EditText fieldTaskDate;
        private string taskDate;
        private CalendarView cal;
        private SimpleDateFormat sdf;
        private long taskDateLong;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.GoogleChart);
            cal = FindViewById<CalendarView>(Resource.Id.calendarView);
            fieldTaskDate = FindViewById<EditText>(Resource.Id.editText_TaskDate);
            sdf = new SimpleDateFormat("dd MMM yyyy");



            Button b = FindViewById<Button>(Resource.Id.button_Cal);
            b.Click += delegate(object sender, EventArgs args)
            {
                AlertDialog calDialog = DateDialogBox();
                calDialog.Show();

                Calendar date = Calendar.Instance;
                string currDate = sdf.Format(date.Time);
                calDialog.SetTitle(currDate);
                taskDate = currDate;
                taskDateLong = date.TimeInMillis;
                Button yesBtn = calDialog.GetButton((int)DialogButtonType.Positive);
                Button noBtn = calDialog.GetButton((int)DialogButtonType.Negative);
                //yesBtn.Click += (s, e) =>
                //{
                //    GregorianCalendar gCal = new GregorianCalendar();
                //    gCal.Add(CalendarField.Date, -1);
                //    long lMillis = gCal.Time.Time;

                //    fieldTaskDate.Text = taskDate;
                //    calDialog.Dismiss();
                //};
                noBtn.Click += (s, e) => calDialog.Dismiss();
                cal.SetOnDateChangeListener(this);
            };
        }       
        private AlertDialog DateDialogBox()
        {
            LayoutInflater inflater = this.LayoutInflater;
            View view = inflater.Inflate(Resource.Layout.CalendarDialog, null);

            AlertDialog.Builder dateDialogBuilder = new AlertDialog.Builder(this);
            dateDialogBuilder.SetTitle("Date");
            dateDialogBuilder.SetView(view);

            dateDialogBuilder.SetPositiveButton("SetDate", (s, e) =>
            {
                GregorianCalendar gCal = new GregorianCalendar();
                gCal.Add(CalendarField.Date, -1);
                long lMillis = gCal.Time.Time;

                fieldTaskDate.Text = taskDate;

            });
            dateDialogBuilder.SetNegativeButton("Cancel", (s, e) =>
            {

            });

            AlertDialog dialog = dateDialogBuilder.Create();

            return dialog;
        }


        public void OnSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth)
        {
            string curdate = sdf.Format(cal.Date);

            taskDate = curdate;
            taskDateLong = cal.Date;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可能希望查看&#34; DatePickerDialog&#34;这正是你想要做的。这是一个例子。 http://developer.xamarin.com/guides/android/user_interface/date_picker/