アプリケーションで「開始日」と「終了日」を取得するために 2 つの日付ピッカーを使用しています。ユーザーが最初にカレンダーの画像ボタンをクリックすると (両方の日付ピッカーの場合)、現在の日付が表示され、その後画像がクリックされると表示されます。ここでも、以前に選択した日付が表示されます。
私の問題は、アプリのリセットボタンが押されたときにのみ、現在の日付を表示するように両方の日付ピッカーを設定したいということです。*それ以外の場合は、以前に選択した日付が表示されます。それを達成するのを手伝ってください。
前もって感謝します。コードは次のとおりです:`
DatePickerDialog.OnDateSetListener from_dateListener,to_dateListener;
.
.
from_dateListener = new OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
factiveDate.set(Calendar.YEAR,year) ;
factiveDate.set(Calendar.MONTH, monthOfYear);
factiveDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if(currentDate.before(factiveDate)||(currentDate.equals(factiveDate)))
{
if(endDateDisplay.getText().toString().isEmpty())
{
updateStartDisplay();
//startPickDate.setClickable(false);
}
else
{
if(factiveDate.before(toDate)||(factiveDate.equals(toDate)))
{
updateStartDisplay();
//startPickDate.setClickable(false);
}
else
{
Toast t=new Toast(getBaseContext());
t=Toast.makeText(Next.this, "To date should be less than or eqaul to from date", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
}
}
else
{
Toast t=new Toast(getBaseContext());
t=Toast.makeText(Next.this, "Please enter a valid date", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
}
};
to_dateListener = new OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
tactiveDate.set(Calendar.YEAR, year);
tactiveDate.set(Calendar.MONTH, monthOfYear);
tactiveDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if(currentDate.before(tactiveDate)||(currentDate.equals(tactiveDate)))
{
Log.d("1st if","true");
if(!startDateDisplay.getText().toString().isEmpty())
{
Log.d("2st if","true");
if(tactiveDate.after(fromDate)||(tactiveDate.equals(fromDate)))
{
Log.d("3st if","true");
updateEndDisplay();
//endPickDate.setClickable(false);
}
else
{
Toast t=new Toast(getBaseContext());
t=Toast.makeText(Next.this, "To date should be greater than or equal to from date",Toast.LENGTH_LONG );
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
}
else
{
updateEndDisplay();
//endPickDate.setClickable(false);
}
}
else
{
Toast t=new Toast(getBaseContext());
t=Toast.makeText(Next.this, "Please enter a valid date", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
}
};
}
private void updateStartDisplay()
{
startDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(factiveDate.get(Calendar.DAY_OF_MONTH)).append("-")
.append(factiveDate.get(Calendar.MONTH)+1).append("-")
.append(factiveDate.get(Calendar.YEAR)).append(" "));
fromDate=factiveDate;
factiveDate=currentDate;
factiveDate=null;
//Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));
}
private void updateEndDisplay()
{
endDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(tactiveDate.get(Calendar.DAY_OF_MONTH)).append("-")
.append(tactiveDate.get(Calendar.MONTH) + 1).append("-")
.append(tactiveDate.get(Calendar.YEAR)).append(" "));
toDate=tactiveDate;
tactiveDate=null;
//Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));
}
/* capture our View elements for the end date function */
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DATE_PICKER_FROM:
Log.d("current date-on create"," "+(currentDate.get(Calendar.YEAR))+ (currentDate.get(Calendar.MONTH)+1)+ (currentDate.get(Calendar.DAY_OF_MONTH)));
return new DatePickerDialog(this, from_dateListener, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
case DATE_PICKER_TO:
return new DatePickerDialog(this, to_dateListener, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
}