我对此很陌生,所以请原谅我的天真。我已经在论坛中正确搜索了这个问题,但找不到解决方案,但可能是没有人像我一样粗心犯错误.. :)
我在两个对话框中调用的表单上有两个按钮。我有一个 Onprepare 方法和 On create 方法,用于通过开关和案例处理的对话框。
public void onClick(View arg0) {
// TODO what needs to be done on button clicks
switch (arg0.getId()) {
case R.id.bAddtrans:
case R.id.btransdate:
showDialog(1);
date.setText(strDate);
case R.id.bpaidfor:
showDialog(2);
}
我的 On Prepare 和 On Create 方法是:
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case 1:
// Some initialization needed.
DatePickerDialog dateDlg = (DatePickerDialog) dialog;
int iDay,
iMonth,
iYear;
Calendar cal = Calendar.getInstance();
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
dateDlg.updateDate(iYear, iMonth, iDay);
break;
case 2:
// Static dialog hence no initialization needed
break;
}
return;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
DatePickerDialog dateDlg = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dtDob = chosenDate.toMillis(true);
strDate = DateFormat.format("MMMM dd, yyyy", dtDob);
// Toast.makeText(this, "Date picked: " + strDate,
// Toast.LENGTH_SHORT).show(); }
}
}, 2011, 0, 1);
dateDlg.setMessage("Please select date..");
/*Toast toast = Toast.makeText(this, "Date picked: " + strDate,
Toast.LENGTH_SHORT);
toast.show();*/
return dateDlg;
//break;
case 2:
// TODO show multiselect dialog box
// ArrayList<String> adapterpaidfor = new ArrayList<String>(Data);
final CharSequence[] peoplelist = Data
.toArray(new CharSequence[Data.size()]);
final ArrayList<CharSequence> selectedpeople = new ArrayList<CharSequence>();
boolean[] checkedpeople = new boolean[peoplelist.length];
int count = peoplelist.length;
for (int i = 0; i < count; i++)
checkedpeople[i] = selectedpeople.contains(peoplelist[i]);
DialogInterface.OnMultiChoiceClickListener peopleDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked)
selectedpeople.add(peoplelist[which]);
else
selectedpeople.remove(peoplelist[which]);
onChangeSelectedpeople();
}
private void onChangeSelectedpeople() {
// TODO change the button name
StringBuilder stringBuilder = new StringBuilder();
for (CharSequence peoplelist : selectedpeople)
stringBuilder.append(peoplelist + ",");
paidfor.setText(stringBuilder.toString());
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select People");
builder.setMultiChoiceItems(peoplelist, checkedpeople,
peopleDialogListener);
builder.setPositiveButton( "OK", onChangeSelectedpeople());
AlertDialog dialog = builder.create();
dialog.show();
return dialog;
//break;
}
return null;
}
private android.content.DialogInterface.OnClickListener onChangeSelectedpeople() {
// TODO Auto-generated method stub
return null;
}
问题是,当我单击按钮 btransdate 时,案例 2 的对话框也会在前端弹出,我必须按对话框的 Ok 按钮,然后我可以在背景上找到具有日期选择器的第二个对话框。当我单击按钮 bpaid 时,一切正常,并且不会调用 datepicker 对话框。