Alternative setButton

2020-05-25 06:48发布

I use this code in my android project:

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

But, Eclipse says that setButton() is deprecated. Please, help me with an alternative solution. Thanks!

标签: android
8条回答
冷血范
2楼-- · 2020-05-25 06:57
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Alert");
            builder.setIcon(R.drawable.ic_launcher);
            builder.setMessage("Attention");
            builder.setPositiveButton("Get Location",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });

            builder.setNeutralButton("Set Location",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {



                        }
                    });

            builder.setNegativeButton("Exit",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
            builder.show();
查看更多
Evening l夕情丶
3楼-- · 2020-05-25 06:58

Use this and set positive or negative button

setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener);

refer this http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int, java.lang.CharSequence, android.content.DialogInterface.OnClickListener)

查看更多
你好瞎i
4楼-- · 2020-05-25 07:11

Here's my short and sweet one, an optimization of Android Developer. It's more concise and uses less memory.

    (new AlertDialog.Builder(/*activity*/))
    .setTitle("ALERTTILESTRING")
    .setMessage("alertNameString")
    .setCancelable(false)
    .setNegativeButton("Close",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    })
    /*insert other Alert Dialog Builder methods here*/
    .show();
查看更多
smile是对你的礼貌
5楼-- · 2020-05-25 07:18

setButton() isn't what is deprecated, but that function + argument combination. There is still setButton(), but you need to give an ID for the button as the first argument for setButton():

alertDialog.setButton(0, "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      <do something>;
    }
});

This is useful if you want to give all your buttons the same OnClickListener:

class alertDialogOnClickListener implements DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {
        switch(which){
            case 1:
                <do something>;
                break;
            case 2:
                <do something>;
                break;
        }
    }
}
alertDialog.setButton(1, "OK", new alertDialogOnClickListener());
alertDialog.setButton(2, "Cancel", new alertDialogOnClickListener());
查看更多
▲ chillily
6楼-- · 2020-05-25 07:20

you can set three kind of buttons with alertDialog

.setPositiveButton("name_of_button",LISTENER);

.setNegativeButton("name_of_button",LISTENER);

.setNeutralButton("name_of_button",LISTENER);

Listener may be outside class or anonymous class like

.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            alertDialog.cancel();
        }
    });
查看更多
小情绪 Triste *
7楼-- · 2020-05-25 07:20

Good post by Tony Stark here and you can beautify your dialog by adding an icon .. Make sure you have the picture in your drawable folder.

 builder.setTitle("Message Sent!").setCancelable(false).setNegativeButton("Close",new DialogInterface.OnClickListener() 
                            {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});
                            AlertDialog alert = builder.create();
                            alert.setIcon(R.drawable.send);//call your image for your icon here
                            alert.show();

Example enter image description here

查看更多
登录 后发表回答