使用DialogBox的Android问题

时间:2014-01-27 12:07:14

标签: android android-button android-dialog

我显示了两个按钮,“GPS设置”和“取消”,我想按下取消按钮时,它会显示另一个带有“确定”按钮的对话框。在这种情况下该怎么办 我使用以下代码..

 if (!isGPSEnabled) {
        // no GPS provider is enabled
        // creating alertdialog
        AlertDialog.Builder builder = new AlertDialog.Builder(c);

        builder.setTitle("Settings");
        builder.setMessage("Enable GPS for the Application");

        builder.setPositiveButton("GPS Setting",

        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                dialog.dismiss();
            }

        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("How to use Application")
                                .setMessage(
                                        "You must enable the GPS in order to use this application. Press Activate and then press Power Button twice in order to send the alert message to the selected contacts")
                                .setNeutralButton(
                                        "OK",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                // do something

                                            /*  Intent inmainact = new Intent(
                                                        getApplicationContext(),
                                                        MainActivity.class);
                                                startActivity(inmainact);   */
                                                 dialog.cancel();

                                            }
                                        }).show();
                        dialog.dismiss();

                    }
                });

        builder.show();

3 个答案:

答案 0 :(得分:2)

将以下代码添加到“取消”按钮单击

new AlertDialog.Builder(MainActivity.this)
        .setTitle("")
        .setMessage("")
        .setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // do something

            }
        }).show();

答案 1 :(得分:0)

 AlertDialog builder = new AlertDialog.Builder(YourActivity.this).create();
                    builder.setTitle("Save as Favourites");
                    builder.setMessage("Your message");

                    builder.setButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int which) {
                       // your task
                        builder.dismiss();           

                        }
                    });


                    builder.setButton2("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                 // your task           
                          builder.dismiss();     

                        }
                    });
                    builder.show();

答案 2 :(得分:0)

检查此代码:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

                            builder.setTitle("Settings");
                            builder.setMessage("Enable GPS for the Application");

                            builder.setPositiveButton("GPS Settings",

                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    //do Something
                                }

                            });

                            builder.setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                              dialog.dismiss();
                                            AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
                                            builder2.setMessage("Look at this dialog!")
                                                   .setCancelable(false)
                                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                       public void onClick(DialogInterface dialog, int id) {
                                                            //do things
                                                            Toast.makeText(getApplicationContext(), 
                                                                    "OnClickListener : " + "OK Clicked",
                                                                    Toast.LENGTH_SHORT).show();
dialog.dismiss();
                                                       }
                                                   });
                                            AlertDialog alert2 = builder2.create();
                                            alert2.show();

                                        }
                                    });

                            AlertDialog alert = builder.create();
                            alert.show();