web-dev-qa-db-fra.com

Ok Bouton pour la boîte de dialogue d'alerte sur Android

Ceci est mon code pour une alerte, il affiche un message lorsque vous appuyez sur le bouton. Comment puis-je créer un bouton OK pour rejeter?

    Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);
            alert.show();
        }
    });
}}      

J'ai mis alert.setCancelMessage (null) mais il n'affiche aucun bouton à ignorer.

Merci pour toutes les réponses.

Solution:

      Button bm1 = (Button) findViewById(R.id.button1);
       bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Builder alert = new AlertDialog.Builder(screen4.this);
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setPositiveButton("OK", null);
            alert.show();
        }
    });
27
berk kaan

Modifiez votre dialogue d'alerte en ceci:

Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();    
44
Misha Bhardwaj

Vous pouvez créer à la fois le bouton OK et le bouton Annuler pour la boîte de dialogue en utilisant ceci,

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getApplicationContext());
                builder.setCancelable(true);
                builder.setTitle("Title");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
11
Sahil Mahajan Mj

Cela ne pourrait-il pas être fait en utilisant uniquement un bouton positif?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();
3
Xar E Ahmer

Vous pouvez le faire en utilisant setPositiveButton et setNegativeButton

    alert.setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            //Do something
            alert.dismiss();
        }
    });
    alert.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            alert.dismiss();
        }
    });

Édité:

alert.setButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        // Do something
        alert.dismiss();
        }
    });
alert.setButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

        alert.dismiss();
        }
    });
1
MysticMagicϡ

Vérifiez la page AlertDialog.Builder dans l'API: http://developer.Android.com/reference/Android/app/AlertDialog.Builder.html

Vous pouvez définir des boutons positifs, neutres et négatifs à l'aide des méthodes setPositiveButton, setNeutralButton et setNegativeButton, respectivement

0
Dhanesh Budhrani
    Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final AlertDialog dialog = new AlertDialog.Builder(screen4.this).create();
    dialog.setTitle("Doctor");
    dialog.setMessage("message");
    dialog.setButton("Ok",new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            // here you can add functions

           }
    });
    dialog.setButton2("Cancel",new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        // here you can add functions
               dialog.dismiss();
    }
    });
    dialog.show();
    }};      
0
Praveen
    bt = (Button) findViewById(R.id.button1);
    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ab = new AlertDialog.Builder(MainActivity.this);

            ab.setTitle("confirm");
            ab.setIcon(R.drawable.ic_launcher);
            ab.setMessage("Do u want to close this application?");

            ab.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
                }
            });

            ab.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "cancle", Toast.LENGTH_SHORT).show();
                }
            });

            ab.show();
        }
    });
0
siva

alert.setPositiveButton ou alert.setNegativeButton

0
Opiatefuchs
Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    //do somthing or dismiss dialog by                dialog.dismiss();

                }
            });

            alert.show();
        }
    });
}}      
0
Manmeet Singh Batra