其他分享
首页 > 其他分享> > Android DatePicker:根据CURRENT日期禁用PREVIOUS日期

Android DatePicker:根据CURRENT日期禁用PREVIOUS日期

作者:互联网

我的应用程序中有一个DatePicker,它以这种方式设置:

布局:

            <TextView
                android:id="@+id/traveltext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.11"
                android:text="Date Of Travel"
                android:textStyle="bold" />

             <EditText
            android:id="@+id/txtDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </EditText>

              <Button
            android:id="@+id/btnCalendar"
            android:layout_width="30dp"
            android:layout_height="42dp"
            android:layout_marginRight="50dp"
            android:background="@drawable/date" >
        </Button>
        </LinearLayout>

全球价值观:

Button btnCalendar;
EditText txtDate;
private int mYear, mMonth, mDay;

OnCreate:

btnCalendar = (Button) findViewById(R.id.btnCalendar);
    txtDate = (EditText) findViewById(R.id.txtDate);
    btnCalendar.setOnClickListener(this);

方法 :

 @Override
public void onClick(View v) {

    if (v == btnCalendar) {

        // Process to get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // Launch Date Picker Dialog
        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        // Display Selected date in textbox
                        txtDate.setText(dayOfMonth + "-"
                                + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        dpd.show();
    }   }

现在我的问题是,如何根据CURRENT日期禁用PREVIOUS日期?

请帮忙.谢谢.

解决方法:

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    // the callback received when the user "sets" the Date in the
    // DatePickerDialog
    public void onDateSet(DatePicker view, int yearSelected,
            int monthOfYear, int dayOfMonth) {
        year = yearSelected;
        month = monthOfYear + 1;
        day = dayOfMonth;
        // Set the Selected Date in Select date Button
        txtarrivedate.setText(year + "-" + month + "-" + day);
    }
};

//当调用showDialog()方法时,方法会自动调用

   @Override
   protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:
        // create a new DatePickerDialog with values you want to show

        DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
                mYear, mMonth, mDay);
        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, 1);
        Date newDate = c.getTime();
        da.getDatePicker().setMinDate(newDate.getTime());
        return da;
        // create a new TimePickerDialog with values you want to show
    case 2:
        // create a new DatePickerDialog with values you want to show
        DatePickerDialog da1 = new DatePickerDialog(this,
                mDateSetListener2, mYear, mMonth, mDay);
        Calendar c1 = Calendar.getInstance();

        c1.add(Calendar.DATE, 1);
        Date newDate2 = c1.getTime();
        da1.getDatePicker().setMinDate(newDate2.getTime());
        return da1;

    }
    return null;
}

仅使用用于调用对话框
的ShowDialog(1);

标签:android-datepicker,android
来源: https://codeday.me/bug/20191002/1843741.html