本文介绍了DatePickerDialog始终打开当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有麻烦 DatePickerDialog
,每次我打开它显示当前日期不是我设置previously的日期。
I am having trouble with a DatePickerDialog
, everytime I open it shows the current date not the date I set previously.
下面是我的code:
final Calendar c = Calendar.getInstance();
final int mYear = c.get(Calendar.YEAR);
final int mMonth = c.get(Calendar.MONTH);
final int mDay = c.get(Calendar.DAY_OF_MONTH);
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) {
editText.setText(selectedYear + "年"
+ selectedMonth + 1 + "月"
+ selectedDay + "日");
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(SampleActivity.this,
datePickerListener, mYear, mMonth, mDay);
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.cancel();
}
}
});
datePickerDialog.setCancelable(false);
datePickerDialog.show();
我错过了什么吗?
What did I miss here?
推荐答案
这可以帮助你:
public class TestActivity extends Activity {
private int mYear;
private int mMonth;
private int mDay;
private void showDialog() {
if (mYear == 0 || mMonth == 0 || mDay == 0) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
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) {
editText.setText(selectedYear + "年" + selectedMonth + 1 + "月"
+ selectedDay + "日");
mYear = selectedYear;
mMonth = selectedMonth;
mDay = selectedDay;
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(
SampleActivity.this, datePickerListener, mYear, mMonth, mDay);
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.cancel();
}
}
});
datePickerDialog.setCancelable(false);
datePickerDialog.show();
}
}
下面的示例code会给完整的解决方案:
Below sample code will give complete solution:
public class SecondActivity extends FragmentActivity implements OnClickListener{
private static final String TAG = SecondActivity.class.getName();
private int year;
private int month;
private int day;
private boolean isCancelled;
private EditText editText;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.second_activity_layout);
editText = (EditText) findViewById(R.id.editText);
}
public void onButtonClick(View view) {
onClick(view);
}
@Override
public void onClick(View v) {
if(year == 0) {
Calendar calendar = Calendar.getInstance();
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
year = calendar.get(Calendar.YEAR);
}
OnDateSetListener callBack = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
if(isCancelled) {
Log.i(TAG, "Cancelled");
isCancelled = false;
} else {
editText.setText("Date selected");
month = monthOfYear;
day = dayOfMonth;
SecondActivity.this.year = year;
}
}
};
DatePickerDialog dialog = new DatePickerDialog(this, callBack, year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEGATIVE) {
Log.i(TAG, "Cancel");
isCancelled = true;
dialog.dismiss();}
}
});
dialog.setCancelable(false);
dialog.show();
}
}
这篇关于DatePickerDialog始终打开当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!