0

我想在警报对话框上单击“确定”按钮执行一些操作,我正在使用带有 1 个操作按钮的 AlertDialog。当我尝试添加一个 netral/positive/negative 按钮时,它向我显示了两个按钮,请帮忙。

4

2 回答 2

1
btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Creating alert Dialog with two Buttons

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Confirm Delete...");

            // Setting Dialog Message
            alertDialog.setMessage("Are you sure you want delete this?");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.delete);

            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which) {
                            // Write your code here to execute after dialog
                            Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                        }
                    });
            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Write your code here to execute after dialog
                            Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

        }
    });



btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Creating alert Dialog with three Buttons

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    AlertDialogActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Save File...");

            // Setting Dialog Message
            alertDialog.setMessage("Do you want to save this file?");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.save);

            // Setting Positive Yes Button
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // User pressed Cancel button. Write Logic Here
                            Toast.makeText(getApplicationContext(),
                                    "You clicked on YES",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            // Setting Negative No Button... Neutral means in between yes and cancel button
            alertDialog.setNeutralButton("NO",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // User pressed No button. Write Logic Here
                            Toast.makeText(getApplicationContext(),
                                    "You clicked on NO", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
            // Setting Positive "Cancel" Button
            alertDialog.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // User pressed Cancel button. Write Logic Here
                            Toast.makeText(getApplicationContext(),
                                    "You clicked on Cancel",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            // Showing Alert Message
            alertDialog.show();

        }
    });
于 2013-09-12T10:01:17.373 回答
0

您只需要设置肯定按钮。尝试这个:

alertDialogBuilder.setMessage("Your username or password is incorrect! Please try again!").setCancelable(false)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // if this button is clicked, close
                                        // current activity
                                        // MainActivity.this.finish();
                                        dialog.cancel();
                                    }
                                });
于 2013-12-16T08:45:41.747 回答