我想在单击编辑文本时打开一个日历。之后,我想在编辑文本中设置用户从日历中选择的日期。问题是,只有当我第二次单击 EditText 时,日历才会打开。请帮我解决问题(why calendar don't open for the first time
)。
编辑文本XML 代码
<EditText
android:id="@+id/dateofBirth"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:maxLines="1"
android:hint="dd/mm/yyyy" />
活动代码
public void informationPopUp() {
final Dialog dialog= new Dialog(MainActivity.this,R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.details_dialog);
dateofBirth = (EditText)dialog.findViewById(R.id.dateofBirth);
dialog.show();
myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dateofBirth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dateofBirth.setText(sdf.format(myCalendar.getTime()));
}
protected Dialog onCreateDialog(int id) {
final Calendar now = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, date,myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){
if (year > now.get(Calendar.YEAR))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) &&
monthOfYear == now.get(Calendar.MONTH))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
}
};
return _date;
}
return null;
}
我不明白我做错了什么。请提出解决方案。