簡體   English   中英

如何使用1個操作按鈕對警報對話框執行一些操作

[英]how to perform some action on alert dialogue box with 1 action button

我想在單擊警報對話框上的“確定”按鈕時執行一些操作,正在使用帶有1個操作按鈕的AlertDialog。 當我嘗試添加一個凈/正/負按鈕時,它向我顯示了兩個按鈕,請幫忙。

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();

        }
    });

您只需要設置肯定按鈕即可。 嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM