我知道之前可能已经提出这个问题了,但我无法找到解决问题的方法。我有两个DatePickerDialogs,其中,无论何时设置开始日期,我都会将另一个datepickerdialog的最小日期设置为该日期。但它仍然没有工作,因为enddatepicker的最小日期与startdatepicker相同。这是我的参考代码:
Calendar c = Calendar.getInstance();
selectedEndDay = selectedStartDay = c.get(Calendar.DAY_OF_MONTH);
selectedEndMonth = selectedStartMonth = c.get(Calendar.MONTH);
selectedEndYear = selectedStartYear = c.get(Calendar.YEAR);
startDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
selectedStartYear = year;
selectedStartMonth = monthOfYear;
selectedStartDay = dayOfMonth;
Log.e(TAG, "SET DATE: " + selectedStartYear + "/" + selectedStartMonth + "/" + selectedStartDay);
btnStartDate.setText((selectedStartMonth + 1) + "/" + selectedStartDay + "/" + selectedStartYear);
//set minimum date of end datepicker
Calendar c2 = Calendar.getInstance();
c2.set(year, monthOfYear, dayOfMonth);
endDatePickerDialog.getDatePicker().setMinDate(c2.getTime().getTime());
}
},selectedStartYear,selectedStartMonth,selectedStartDay);
startDatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);
endDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
selectedEndYear = year;
selectedEndMonth = monthOfYear;
selectedEndDay = dayOfMonth;
Log.e(TAG, "SET DATE: " + selectedEndYear + "/" + selectedEndMonth + "/" + selectedEndDay);
btnEndDate.setText((selectedEndMonth+1)+"/"+selectedEndDay+"/"+selectedEndYear);
}
},selectedEndYear,selectedEndMonth,selectedEndDay);
//set minimum date of end datepicker
endDatePickerDialog.getDatePicker().setMinDate(c.getTime().getTime());
答案 0 :(得分:1)
我在下面演示使用此代码。
这是类文件。
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
public class Example extends Activity {
private EditText startDate, endDate;
private int selectedStartYear;
private int selectedStartDay;
private int selectedStartMonth;
private int selectedEndDay;
private int selectedEndMonth;
private int selectedEndYear;
private DatePickerDialog SecD, firstD;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
startDate = (EditText) findViewById(R.id.editText1);
endDate = (EditText) findViewById(R.id.editText2);
final Calendar c = Calendar.getInstance();
selectedEndDay = selectedStartDay = c.get(Calendar.DAY_OF_MONTH);
selectedEndMonth = selectedStartMonth = c.get(Calendar.MONTH);
selectedEndYear = selectedStartYear = c.get(Calendar.YEAR);
SecD = new DatePickerDialog(Pdf.this, new SecondDate(),
selectedEndYear, selectedEndMonth, selectedEndDay);
firstD = new DatePickerDialog(Pdf.this, new FirstDate(),
selectedStartYear, selectedStartMonth, selectedStartDay);
startDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
firstD.show();
}
});
endDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SecD.show();
}
});
}
class FirstDate implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Doing thing with first Date pick Dialog
selectedStartYear = year;
selectedStartMonth = monthOfYear;
selectedStartDay = dayOfMonth;
startDate.setText((selectedStartMonth + 1) + "/" + selectedStartDay
+ "/" + selectedStartYear);
// set minimum date of end datepicker
Calendar c2 = Calendar.getInstance();
c2.set(year, monthOfYear, dayOfMonth);
SecD.getDatePicker().setMinDate(c2.getTime().getTime());
}
}
class SecondDate implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Doing thing with second Date Picker Dialog
selectedEndYear = year;
selectedEndMonth = monthOfYear;
selectedEndDay = dayOfMonth;
endDate.setText((selectedEndMonth + 1) + "/" + selectedEndDay + "/"
+ selectedEndYear);
}
}
}
这是xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>