How to disable dates before today date in DatePickerDialog Android?

前端 未结 5 984
深忆病人
深忆病人 2020-11-30 07:18

I want to disable the dates before today date in the DatePickerDialog .I am new in android please suggest me how could i do this .Here is my code that i have written for Dat

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:31

    See this example..!

    import java.util.Calendar;
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TextView;
    
    public class MyAndroidAppActivity extends Activity {
    
        private TextView tvDisplayDate;
    
        private Button btnChangeDate;
    
        private int myear;
        private int mmonth;
        private int mday;
    
        static final int DATE_DIALOG_ID = 999;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            setCurrentDateOnView();
            addListenerOnButton();
    
        }
    
        // display current date
        public void setCurrentDateOnView() {
    
            tvDisplayDate = (TextView) findViewById(R.id.tvDate);       
    
            final Calendar c = Calendar.getInstance();
            myear = c.get(Calendar.YEAR);
            mmonth = c.get(Calendar.MONTH);
            mday = c.get(Calendar.DAY_OF_MONTH);
    
            // set current date into textview
            tvDisplayDate.setText(new StringBuilder()
                    // Month is 0 based, just add 1
                    .append(mmonth + 1).append("-").append(mday).append("-")
                    .append(myear).append(" "));
        }
    
        public void addListenerOnButton() {
    
            btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
    
            btnChangeDate.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    showDialog(DATE_DIALOG_ID);
    
                }
    
            });
    
        }
    
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
                // set date picker as current date
                DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, myear,mmonth,
                        mday){
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                    {   
                        if (year < myear)
                            view.updateDate(myear, mmonth, mday);
    
                        if (monthOfYear < mmonth && year == myear)
                            view.updateDate(myear, mmonth, mday);
    
                        if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                            view.updateDate(myear, mmonth, mday);
    
                    }
                };
                return _date;
            }
            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) {
                myear = selectedYear;
                mmonth = selectedMonth;
                mday = selectedDay;
    
                // set selected date into textview
                tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
                        .append("-").append(mday).append("-").append(myear)
                        .append(" "));    
    
            }
        };
    
    }
    

    main.xml

    
    
    
        

提交回复
热议问题