Android 日期选择器禁用 2014 年 12 月 1 日之前的所有日期

Android datepicker disable all dates prior to 1st dec 2014

我已经在 android 中成功实现了 datepicker 我的问题是我想在 2014 年 12 月 1 日之前禁用 datepicker 中的所有日期如何做到这一点,我尝试寻找示例但他们都提供了示例禁用今天日期之前的日期

我还没有实现任何代码来禁用以前的日期任何工作示例都会很好 提前致谢

这是我的代码

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

     /** Private members of the class */
    private TextView pDisplayDate;
    private Button pPickDate;
    private int pYear;
    private int pMonth;
    private int pDay;
    /** This integer will uniquely define the dialog to be used for displaying date picker.*/
    static final int DATE_DIALOG_ID = 0;

    /** Callback received when the user "picks" a date in the dialog */
    private DatePickerDialog.OnDateSetListener pDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    pYear = year;
                    pMonth = monthOfYear + 1;
                    pDay = dayOfMonth;

//                    if (pMonth<10)
//                         pMonth='0'+pMonth;
//                       if (pDay<10)
//                         pDay='0'+pDay;


                    updateDisplay();
                    displayToast();
                }
            };

    /** Updates the date in the Te
     * xtView */
    private void updateDisplay() {

        String formattedMonth = "" + pMonth;
        String formattedDayOfMonth = "" + pDay;

        if(pMonth < 10){

            formattedMonth = "0" + pMonth;
        }
        if(pDay < 10){

            formattedDayOfMonth = "0" + pDay;
        }
       // searchText.setText(formattedDayOfMonth + "/" + formattedMonth + "/" + year);


        pDisplayDate.setText(
            new StringBuilder()
//                    // Month is 0 based so add 1
                    .append(pYear).append("-")
                    .append(formattedMonth).append("-")
                    .append(formattedDayOfMonth).append(" "));
    }

    /** Displays a notification when the date is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()),  Toast.LENGTH_SHORT).show();

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Capture our View elements */
        pDisplayDate = (TextView) findViewById(R.id.displayDate);
        pPickDate = (Button) findViewById(R.id.pickDate);

        /** Listener for click event of the button */
        pPickDate.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        /** Get the current date */
        final Calendar cal = Calendar.getInstance();
        pYear = cal.get(Calendar.YEAR);
        pMonth = cal.get(Calendar.MONTH);
        pDay = cal.get(Calendar.DAY_OF_MONTH);

        /** Display the current date in the TextView */
        updateDisplay();
    }

    /** Create a new dialog for date picker */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, 
                        pDateSetListener,
                        pYear, pMonth, pDay);
        }
        return null;
    }
} 

像这样设置最大日期:

protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
DatePickerDialog dialog = new DatePickerDialog(mContext,
                        datePickerListener, myCalendar.get(Calendar.YEAR),
                        myCalendar.get(Calendar.MONTH), myCalendar
                                .get(Calendar.DAY_OF_MONTH));
             dialog.getDatePicker().setMaxDate(new Date().getTime());
                    dialog.show();
        }
        return null;
    }

完成了,这可能对可以相应地使用 if 条件的人有所帮助

包 com.example.datepicker;

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 MainActivity extends Activity {

    private TextView tvDisplayDate;

    private Button btnChangeDate;

    private int myear;
    private int mmonth;
    private int mday;

    private int maxyear;
    private int maxmonth;
    private int maxday;

    static final int DATE_DIALOG_ID = 999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setCurrentDateOnView();
        addListenerOnButton();

    }

    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);       

        final Calendar c = Calendar.getInstance();
        maxyear = c.get(Calendar.YEAR);
        maxmonth = c.get(Calendar.MONTH);
        maxday = c.get(Calendar.DAY_OF_MONTH);
        myear = 2014;
        mmonth = 12;
        mday = 31;

        String formattedMonth = "" + mmonth;
        String formattedDayOfMonth = "" + mday;

        // set selected date into textview
        if(maxmonth < 10){

            formattedMonth = "" + maxmonth + 1;
        }
        if(maxday < 10){

            formattedDayOfMonth = "0" + maxday;
        }




        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(formattedMonth).append("-").append(formattedDayOfMonth).append("-")
        .append(maxyear).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, maxyear,maxmonth,
                    maxday){
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {   
                    if (year < 2014)
                        view.updateDate(2014, mmonth, mday);

                    if (monthOfYear < 12 && year == 2014)
                        view.updateDate(2014, 12, mday);

                    if (dayOfMonth < 31 && year == 2014 && monthOfYear == 12)
                        view.updateDate(2014, 12, 31);

                    if (year > maxyear)
                        view.updateDate(maxyear, maxmonth, maxday);

                    if (monthOfYear > maxmonth && year == maxyear)
                        view.updateDate(maxyear, maxmonth, maxday);

                    if (dayOfMonth > maxday && year == maxyear && monthOfYear == maxmonth)
                        view.updateDate(maxyear, maxmonth, maxday);

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

            String formattedMonth = "" + mmonth;
            String formattedDayOfMonth = "" + mday;

            // set selected date into textview
            if(mmonth < 10){

                formattedMonth = "" + mmonth + 1;
            }
            if(mday < 10){

                formattedDayOfMonth = "0" + mday;
            }



            tvDisplayDate.setText(new StringBuilder().append(formattedMonth)
                    .append("-").append(formattedDayOfMonth).append("-").append(myear)
                    .append(" "));    

        }
    };

}