DatePicker对话当前日期无法选择

时间:2016-06-22 06:24:00

标签: android android-datepicker

您好,我是新的Android开发人员。在我的应用程序中,我创建了DatePickerDialogue

我的问题是,当我选择除当前日期之外的任何日期时,当前日期无法选择。

我的代码:

public class DatePickerDialogue extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.shared_layout);

    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);

    DatePickerDialog dpd = new DatePickerDialog(DatePickerDialogue.this,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                }

            }, mYear, mMonth, mDay);

    dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
    dpd.show();
}

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

使用这种方式

在下面的代码中,我OnclickListener上有TextView,并Date设置为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);

        final TextView textView = (TextView)findViewById(R.id.textview23);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog dpd = new DatePickerDialog(MainActivity.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());
                                textView.setText(date);

                                mYear = c.get(Calendar.YEAR);
                                mMonth = c.get(Calendar.MONTH);
                                mDay = c.get(Calendar.DAY_OF_MONTH);
                            }
                        }, mYear, mMonth, mDay);
                dpd.getDatePicker().setMaxDate(System.currentTimeMillis());

                Calendar d = Calendar.getInstance();

                dpd.updateDate(d.get(Calendar.YEAR),d.get(Calendar.MONTH),d.get(Calendar.DAY_OF_MONTH));
                dpd.show();


            }

        });

禁用未来日期:

您应该可以在DatePickerDialog上调用getDatePicker()。setMaxDate(long),将今天设置为最大日期。它将disable未来Date

更新:

要重置Date,只要您想要默认Date,您必须更新它是默认值。如下面的代码,您可以设置它。

dpd.updateDate(d.get(Calendar.YEAR),d.get(Calendar.MONTH),d.get(Calendar.DAY_OF_MONTH));

希望这会对你有所帮助。

答案 1 :(得分:0)

试试这个

   Calendar cal=Calendar.getInstance();
   int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    int day=cal.get(Calendar.DAY_OF_WEEK);

    DatePickerDialog dp=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

        }
    },year,month,day);
dp.getDatePicker().setMaxDate(cal.getTimeInMillis());
 dp.show();
相关问题