Android 重新选择日期选择器后的日期选择器对话框显示当前日期

Android 重新选择日期选择器后的日期选择器对话框显示当前日期,android,datepicker,datepickerdialog,Android,Datepicker,Datepickerdialog,再次选择时,我试图在datepicker对话框中显示所选日期 这是我试过的 signup.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Calendar mcurrentDate=Calendar.getInstance(); year=mcurrentDate.get(Calen

再次选择时,我试图在datepicker对话框中显示所选日期

这是我试过的

 signup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;
                     signup.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
                mDatePicker.show();


        }
    });
但问题是,当我再次选择约会对象时。它显示当前日期。 我想在对话框中显示所选日期。

您可以使用:

private EditText mbirthdayView;  
    mbirthdayView=(EditText)findViewById(R.id.birthday);
mbirthdayView.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
        }
    });

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, year, month,
                    day);
        }
        return null;
    }

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            mbirthdayView.setText(  (day)+(".")+(month + 1)+(".")+(year));

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);

        }
    };
您可以使用以下选项:

private EditText mbirthdayView;  
    mbirthdayView=(EditText)findViewById(R.id.birthday);
mbirthdayView.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
        }
    });

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, year, month,
                    day);
        }
        return null;
    }

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            mbirthdayView.setText(  (day)+(".")+(month + 1)+(".")+(year));

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);

        }
    };

你可以效仿我的例子:

打开“res/layout/main.xml”文件,添加日期选择器、标签和按钮进行演示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我希望你能从我的例子中得到帮助。

你可以效仿我的例子:

打开“res/layout/main.xml”文件,添加日期选择器、标签和按钮进行演示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我希望你能从我的例子中得到帮助。

我已经解决了答案

答案是

public void onClick(View v) {

             if (year == 0 || month == 0 || day == 0) {
             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
             }

                DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;
                     signup.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
                mDatePicker.show();
我跟着那一页走


阅读评论并感谢@Andrew T:

我已经解决了答案

答案是

public void onClick(View v) {

             if (year == 0 || month == 0 || day == 0) {
             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
             }

                DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;
                     signup.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
                mDatePicker.show();
我跟着那一页走


阅读评论并感谢@Andrew T:

我想第二次你不会从DatePicker中选择日期,对吗?@user3467110你可以在下面看到我的答案,如果检查了@ErsinGülbahar和thnx…@user3467110,如果它不起作用,你可以告诉我,在我的环境中,它是有效的。我想第二次你不会从日期选择器中选择日期,对吗?@user3467110你可以在下面看到我的答案。检查@ErsinGülbahar和thnx…@user3467110如果它不起作用,你可以告诉我,在我的环境中,它是有效的,用于给我反馈@omor4android,但我想从我的源代码中得到答案a/c…谢谢你给我反馈@omor4android,但我想从我的源代码中得到答案。。。