简体   繁体   English

在单个调用android上弹出多个对话框

[英]Multiple dialog popping up on a single call android

I am pretty new to this so please forgive for my naivety. 我对此很新,所以请原谅我的天真。 I have properly searched this problem in the forum and couldnot find the resolution, but then it could be that no one has been careless enough to make a mistake like me .. :) 我已经在论坛中正确搜索了这个问题并且找不到解决方案,但是可能没有人像我这样犯错了...... :)

I have two buttons on a form from where I am calling in two dialog boxes. 我在一个表单上有两个按钮,我在两个对话框中调用。 I have an Onprepare method and On create method for the dialog which I am handling via a switch and case. 我有一个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);
    }

My On Prepare and On Create methods are : 我的准备和创建方法是:

@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;
}

The Problem is that when i click on the button btransdate the dialog for case 2 also pops up on the frontend I have to press Ok button of the dialog and then I can find my 2nd dialog box on the background having the datepicker. 问题是,当我点击按钮btransdate时,前端弹出的对话框也会弹出对话框,我必须按下对话框的确定​​按钮,然后我可以在背景上找到我的第二个对话框,其中有日期选择器。 When i click the button bpaidfor every thing work fine and the datepicker dialog box is not called. 当我点击按钮bpaidfor每个工作正常,并没有调用datepicker对话框。

In your first block of code, you are missing your break statements. 在您的第一个代码块中,您缺少break语句。 You are falling through the switch statement and subsequently making all of the called before returning. 您正在通过switch语句,然后在返回之前完成所有调用。

In your onClick method you should write a break; 在你的onClick方法中,你应该写一个休息; command after each case, like this: 每个案例后命令,如下所示:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM