我在我的应用程序中使用了datepicker对话框,其中我设置了最小和最大日期工作正常。设置按钮在编辑文本中设置所选日期,但取消按钮也可用作设置按钮,并且不会返回上一个保存日期。 请有人帮助我。谢谢你。
以下是代码:
private String dateToSave;
DatePickerDialog _date = null;
private DatePicker dpResult;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
int minYear1 = 1970;
int minMonth1 = 0;//0-january , 1-february , 2-march..
int minDay1 =1;
int minYear = minYear1;
int minMonth = minMonth1;
int minDay = minDay1;
//these are the minimum dates to set Datepicker..
private int year;
private int month;
private int day;
public String dateOutput=null;
@SuppressLint("NewApi")
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_change_profile);
dobET = (EditText) findViewById(R.id.dobET);
myCalendar = Calendar.getInstance();
mYear = myCalendar.get(Calendar.YEAR);
mMonth = myCalendar.get(Calendar.MONTH);
mDay = myCalendar.get(Calendar.DAY_OF_MONTH);
setCurrentDateOnView();
dobET.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
public void setCurrentDateOnView() {
System.out.println("----------setCurrentDateOnView()-----------");
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
// set current date into datepicker
//dpResult.init(year, month, day, null);
}
//this was the main part in program to Restrict the DatePicker Dialog to only its current date.
@Override
protected Dialog onCreateDialog(int id) {
System.out.println("----------onCreateDialog()-----------");
switch (id) {
case DATE_DIALOG_ID:
_date = new DatePickerDialog(this, date,
year, mMonth, mDay){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
System.out.println("----------onDateChanged()-----------"+mYear+"--"+year);
System.out.println("----------onDateChanged()-----------"+mMonth+"--"+monthOfYear);
System.out.println("----------onDateChanged()-----------"+mDay+"--"+dayOfMonth);
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
/* These lines of commented code used for only setting the maximum date on Date Picker..
*
* if (year > mYear && year)
view.updateDate(mYear, mMonth, mDay);
if (monthOfYear > mMonth && year == mYear )
view.updateDate(mYear, mMonth, mDay);
if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear, mMonth, mDay);*/
//these below lines of code used for setting the maximum as well as minimum dates on Date Picker Dialog..
if (year < minYear)
view.updateDate(minYear, minMonth, minDay);
if (monthOfYear < minMonth && year == minYear )
view.updateDate(minYear, minMonth, minDay );
if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
view.updateDate(minYear, minMonth, minDay);
if (year > mYear)
view.updateDate(mYear, mMonth, mDay);
if (monthOfYear > mMonth && year == mYear)
view.updateDate(mYear, mMonth, mDay);
if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear, mMonth, mDay);
dateOutput = String.format("Date Selected: %02d/%02d/%04d",
dayOfMonth, monthOfYear+1, year);
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
}
return _date;
}
private void updateLabel() {
String myFormat = "MM-dd-yyyy"; // In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
String myFormat2 = "yyyy-MM-dd"; // In which you need put here
SimpleDateFormat sdf2 = new SimpleDateFormat(myFormat2, Locale.US);
dateToSave = sdf2.format(myCalendar.getTime());
saveDOBPrefEditor.putString("DOB", dateToSave);
saveDOBPrefEditor.commit();
dobET.setText(sdf.format(myCalendar.getTime()));
}
答案 0 :(得分:0)
我附加了一个代码,用于在按钮上显示所选日期(当您单击此按钮并在其上显示所选日期时,日期对话框将打开)。
使用editText。
代替按钮问题在于updateLabel()。你不应该在onDateChange()里面调用。因为当您更改日期时,onDateChanged()方法会执行,即使您不想设置日期并取消对话框,到那时将调用updateLable()。
如果上述方法无效,请尝试使用以下代码,这对我来说很合适。
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Date Button Instance
btnChangeDate = (Button) findViewById(R.id.makepayact_btn_ChangeDate);
//Date picker Dialog
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textView
btnChangeDate.setText("Select");
}
//Button Listener
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.makepayact_btn_ChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@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
return new DatePickerDialog(this, datePickerListener, year, month,day);
}
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) {
//getting current date instance
final Calendar c = Calendar.getInstance();
int current_year = c.get(Calendar.YEAR);
int current_month = c.get(Calendar.MONTH);
int current_day = c.get(Calendar.DAY_OF_MONTH);
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Cheque Date Validation -------> Keep or check your validations here
if(year<current_year || (year == current_year && month < current_month) || (month == current_month && year == current_year && day < current_day) ){
//Display in a Dialog for Error
AlertDialog.Builder builder = new AlertDialog.Builder(CustomerMgmtActivity_MakePayment.this);
//Chain together various setter methods to set the dialog characteristics
builder.setMessage("Cheque date should not be before today.")
.setTitle("Cheque Date Error!");
// Add the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
dialog.cancel();
}
});
// Get the AlertDialog from create()
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
else{
// set selected date into TextView
btnChangeDate.setText(new StringBuilder().append(day)
.append("-").append(month + 1).append("-").append(year)
.append(" "));
act_CheqDate = (new StringBuilder().append(year)
.append("-").append(month + 1).append("-").append(day + 1)
.append(" ")).toString();
}
}
};
答案 1 :(得分:0)
因为你在onDateChanged方法和updateLabel()类中使用了updateLAbel(),所以你已经在textview中设置了日期。这意味着每次更改日期时,也会执行updateLabel(),因此即使您没有按下设置按钮,也会设置日期。
解决方案:定义您自己的按钮取消和设置。然后setOnClickListener for Set按钮并移动dobET.setText(sdf.format(myCalendar.getTime()));
这段代码