设置日期选择器对话框android的最小日期

时间:2016-04-12 21:55:57

标签: android

我已经设置了我的datepicker对话框,但无法在我的代码中设置它的最小日期,有人帮助我 这是我的代码:

public class PPlanAppActivity extends AppCompatActivity implements View.OnClickListener,DatePickerDialog.OnDateSetListener{
    public EditText tppdate,tpprtype,tpptaxperiod,tppamntdue,
            tpp1pd,tpp1pa,tpp2pd,tpp2pa,tpp3pd,tpp3pa;

    private int pYear;
    private int pMonth;
    private int pDay;
    static final int DATE_DIALOG_ID = 0;
    public static final String PREFS ="useremail";
    private DatePickerDialog.OnDateSetListener pDateSetListener =
            new DatePickerDialog.OnDateSetListener() {


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

                    updateDisplay();
                    displayToast();
                }
            };

    /** Updates the date in the TextView */
    private void updateDisplay() {
        tpp1pd.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear).append(" "));
        tpp2pd.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear).append(" "));
        tpp3pd.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear).append(" "));
    }


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

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pplan_app);


    tppdate =(EditText)findViewById(R.id.padatetxt);

    long date = System.currentTimeMillis();

    final SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
    String dateString = sdf.format(date);
    tppdate.setText(dateString);

        final Calendar c=Calendar.getInstance();

        tpp1pd=(EditText)findViewById(R.id.pa1stpdate);
        tpp1pd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        tpp2pd=(EditText)findViewById(R.id.pa2ndpdate);
        tpp2pd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        tpp3pd=(EditText)findViewById(R.id.pa3rdpdate);
        tpp3pd.setOnClickListener(new View.OnClickListener() {
            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;
    }

1 个答案:

答案 0 :(得分:0)

像这样创建DatePickerDialog -

    final DatePickerDialog datePickerDialog =  new DatePickerDialog(this,
                            pDateSetListener,
                            pYear, pMonth, pDay);
//Set minimum date
datePickerDialog.getDatePicker().setMinDate(<minimum_date_in_millis_here>);

并显示如下 -

 tpp3pd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                datePickerDialog.show();
            }
        });

在这种情况下无需onCreateDialog()

相关问题