0

我遇到了问题DatePickerDialog,每次打开它都会显示当前日期而不是我之前设置的日期。

这是我的代码:

    final Calendar c = Calendar.getInstance();
    final int mYear = c.get(Calendar.YEAR);
    final int mMonth = c.get(Calendar.MONTH);
    final int mDay = c.get(Calendar.DAY_OF_MONTH);

    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) {
            editText.setText(selectedYear + "年"
                    + selectedMonth + 1 + "月"
                    + selectedDay + "日");
        }
    };

    DatePickerDialog datePickerDialog = new DatePickerDialog(SampleActivity.this,
            datePickerListener, mYear, mMonth, mDay);

    datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        dialog.cancel();
                    }
                }
            });

    datePickerDialog.setCancelable(false);
    datePickerDialog.show();

我在这里错过了什么?

4

3 回答 3

2

这可能会帮助您:

public class TestActivity extends Activity {
private int mYear;
private int mMonth;
private int mDay;

private void showDialog() {
    if (mYear == 0 || mMonth == 0 || mDay == 0) {
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    }

    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) {
            editText.setText(selectedYear + "年" + selectedMonth + 1 + "月"
                    + selectedDay + "日");
            mYear = selectedYear;
            mMonth = selectedMonth;
            mDay = selectedDay;
        }
    };

    DatePickerDialog datePickerDialog = new DatePickerDialog(
            SampleActivity.this, datePickerListener, mYear, mMonth, mDay);

    datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        dialog.cancel();
                    }
                }
            });

    datePickerDialog.setCancelable(false);
    datePickerDialog.show();
}
}

下面的示例代码将给出完整的解决方案:

public class SecondActivity extends FragmentActivity implements OnClickListener{

    private static final String TAG = SecondActivity.class.getName();
    private int year;
    private int month;
    private int day;
    private boolean isCancelled;

    private EditText editText;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.second_activity_layout);
        editText = (EditText) findViewById(R.id.editText);
    }

    public void onButtonClick(View view) {
        onClick(view);
    }

    @Override
    public void onClick(View v) {
        if(year == 0) {
            Calendar calendar = Calendar.getInstance();
            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH);
            year = calendar.get(Calendar.YEAR);
        }
        OnDateSetListener callBack = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                if(isCancelled) {
                    Log.i(TAG, "Cancelled");
                    isCancelled = false;
                } else {
                    editText.setText("Date selected");
                    month = monthOfYear;
                    day = dayOfMonth;
                    SecondActivity.this.year = year;
                }
            }
        };
        DatePickerDialog dialog = new DatePickerDialog(this, callBack, year, month, day);
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which == DialogInterface.BUTTON_NEGATIVE) {
                Log.i(TAG, "Cancel");
                isCancelled = true;
                dialog.dismiss();}
            }
        });
        dialog.setCancelable(false);
        dialog.show();
    }
}
于 2013-11-22T05:30:08.310 回答
0

而不是这个,,,

final Calendar c = Calendar.getInstance();
    final int mYear = c.get(Calendar.YEAR);
    final int mMonth = c.get(Calendar.MONTH);
    final int mDay = c.get(Calendar.DAY_OF_MONTH);

使用以下

    String returnedDate = the date which u already set ..



    Date dateToEditApp = new Date();
                SimpleDateFormat newFormatDate = new SimpleDateFormat(
                        "dd MMM yyyy");
                try {
                    dateToEditApp = newFormatDate.parse(returnedDate);
                } catch (ParseException e) {

                    e.printStackTrace();
                    LoggerFile.logwriter("high",TAG + " ParseException "    + e.getMessage());
                }

                Calendar calDate = Calendar.getInstance();

                calDate.setTime(dateToEditApp);

                mYear = calDate.get(Calendar.YEAR);
                mMonth = calDate.get(Calendar.MONTH);
                mDay = calDate.get(Cale

ndar.DAY_OF_MONTH);
于 2013-11-22T05:29:50.100 回答
0

只需在设置日期时将日期、月份和年份值保存到 VARIABLES 中,然后在下一组中使用 ONPREPAREDIALOG 函数在您的 satepicker 对话框中设置先前设置的日期 - 例如 -

private int thisYear, thisMonth, thisday;

private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
           thisYear = year;
           thisMonth = monthOfYear;
           thisday = dayOfMonth;
    }
};

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
          ((DatePickerDialog) dialog).updateDate(thisYear , thisMonth , thisday);
}
于 2013-11-22T06:01:37.357 回答