我有我的應用程序一個DatePicker
這是在這種方式設置:的Android的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;
創建:
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();
} }
現在的問題是,如何禁用基於當前日期的過去日期?
請幫忙。謝謝。
但它顯示此行的'IllegalArgumentException': dpd.getDatePicker()。setMinDate(newDate2.getTime()); – MetaldroiD
dpd.getDatePicker()。setMinDate(System.currentTimeMillis()); – 2014-03-19 06:35:16
你好,檢查上面的代碼,它爲我工作 – 2014-03-19 06:42:19