Android:DatePickerDialog仅输入当前日期而不是选择日期

时间:2015-01-21 14:35:51

标签: java android date datepicker

我正在创建一个日期选择器对话框,用户将在该对话框中选择日期并将其发送到数据库。问题是每当我使用对话框时它只放置当前日期而不是选择日期

代码段:

searchField = (EditText) findViewById(R.id.editText1);  
        final Calendar myCalendar = Calendar.getInstance();

        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

        };
searchField.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

                new DatePickerDialog(Attendance.this, date, myCalendar
                      .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

private void updateLabel() {

        String myFormat = "yyy-MM-dd"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

        searchField.setText(sdf.format(myCalendar.getTime()));
        }

1 个答案:

答案 0 :(得分:0)

以下是我DatePickerDialog onClickListener() textView int mYear, mMonth, mDay; final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); dateView = (TextView) findViewById(R.id.date_view); dateView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DatePickerDialog dpd = new DatePickerDialog(MyActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { c.set(year, month, day); String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime()); dateView.setText(date); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); } }, mYear, mMonth, mDay); dpd.show(); } }); 在您的应用中使用它,效果非常好。

{{1}}