如果我按下对话框外的按钮,对话框将关闭

时间:2017-05-10 20:09:59

标签: android button dialog

我使用以下代码进行对话。我试过alertDialog.setCanceledOnTouchOutside(true);,但它对背景中的按钮无效。

button2.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(final View v)
            {
                if(sp2<100 && finish==0) {
                    sp2++;
                    lbl2.setText(String.valueOf(sp2));
                }
                else
                {
                    finish=1;
                    AlertDialog alertDialog = new AlertDialog.Builder(Multiplayer.this).create();
                    alertDialog.setCanceledOnTouchOutside(true);
                    alertDialog.setTitle("Sieg!");
                    alertDialog.setMessage("Spieler 2 hat das Spiel gewonnen!");
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Erneut spielen?",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Abbrechen",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent myIntent = new Intent(v.getContext(), Startseite.class);
                                    startActivityForResult(myIntent, 0);
                                }
                            });
                    alertDialog.show();
                }
            }
        });

0 个答案:

没有答案