在DatePickerDialog Android中禁用今天日期之前的日期?

时间:2016-02-24 21:27:03

标签: android android-layout android-datepicker

我想在DatePickerDialog中禁用今天日期之前的日期。我是android的新手请建议我怎么做。这是我为DatePickerDialog编写的代码。

 <TextView
            android:id="@+id/tv_arrival"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="@string/arrivaldate"/>

        <Button
            android:id="@+id/btn_arrival"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Set"/>

 Button btn_arrival = (Button) findViewById(R.id.btn_arrival);

        txt_arrival = (TextView) findViewById(R.id.tv_arrival);         
        myCalendar = Calendar.getInstance();          

        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

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

                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                updateLabel();
            }
        };


        btn_arrival.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                 new DatePickerDialog(MainActivity.this,date , myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

            }

        });
private void updateLabel() {

        String myFormat = "dd.MM.yyyy"; //In which you need put here.
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

        txt_arrival.setText("Arrival Date on:" + sdf.format(myCalendar.getTime()));

    }

请告诉我该怎么做。

3 个答案:

答案 0 :(得分:8)

您可以使用此行代码

datePicker.setMinDate(System.currentTimeMillis() - 1000);

你可以像这样使用它。我测试了它,它的工作原理

    import android.app.DatePickerDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;

    public class MainActivity extends AppCompatActivity {

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

        Button mButton = (Button)findViewById(R.id.m_button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog mDate = new DatePickerDialog(MainActivity.this, date, 2016, 2, 24);
                mDate.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                mDate.show();
            }
        });
    }

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            view.setMinDate(System.currentTimeMillis() - 1000);
        }
    };
}

答案 1 :(得分:2)

start_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR); // current year
            int mMonth = c.get(Calendar.MONTH); // current month
            int mDay = c.get(Calendar.DAY_OF_MONTH); // current day

            // date picker dialog
            datePickerDialog1 = new DatePickerDialog(getActivity(),
                    new DatePickerDialog.OnDateSetListener() {

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

                            // set day of month , month and year value in the edit text
                   dept_dayofWeek.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);


                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog1.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); //disable previous dates
            datePickerDialog1.show();
        }
    });

答案 2 :(得分:1)

我测试了代码并且它可以正常工作

final Calendar calendarEndDate = Calendar.getInstance();
YearEndDate = calendarEndDate.get(Calendar.YEAR);
MonthEndDate = calendarEndDate.get(Calendar.MONTH);
DayEndDate = calendarEndDate.get(Calendar.DAY_OF_MONTH);
updateEndDisplay();
endDate.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        DatePickerDialog endDate = new DatePickerDialog(AttendandItemRequest.this,
                endDateSetListener,
                YearEndDate, MonthEndDate, DayEndDate);
        endDate.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        endDate.show();
    }
});