我对此很新,所以请原谅我的天真。 我已经在论坛中正确搜索了这个问题并且找不到解决方案,但是可能没有人像我这样犯了错误......:)
我在表单上有两个按钮,我在两个对话框中调用。 我有一个Onprepare方法和On create方法,用于通过开关和case处理对话框。
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);
}
我的准备和创建方法是:
@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按钮然后我可以在背景上找到我的第二个对话框,其中包含datepicker。 当我点击按钮bpaidfor每个工作正常并且没有调用datepicker对话框。
答案 0 :(得分:2)
在您的第一个代码块中,您缺少break语句。您正在通过switch语句,然后在返回之前完成所有调用。
答案 1 :(得分:0)
在你的onClick方法中,你应该写一个休息;在每个案例之后命令,如下:
public void onClick(View arg0) {
// TODO what needs to be done on button clicks
switch (arg0.getId()) {
case R.id.bAddtrans:
break;
case R.id.btransdate:
showDialog(1);
date.setText(strDate);
break;
case R.id.bpaidfor:
showDialog(2);
break;
}