替代setButton

时间:2012-11-07 10:58:17

标签: android

我在我的android项目中使用此代码:

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

但是,Eclipse说setButton()已被弃用。请帮助我提供替代解决方案。谢谢!

8 个答案:

答案 0 :(得分:67)

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("ALERTTILESTRING")
        .setMessage("alertNameString")
        .setCancelable(false)
        .setNegativeButton("Close",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

我希望上面的代码是一个很好的对话框可以很好地运行

答案 1 :(得分:34)

setButton()不是弃用的东西,而是函数+参数组合。仍然有setButton(),但你需要为按钮提供一个ID作为setButton()的第一个参数:

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

如果您想要为所有按钮提供相同的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());

答案 2 :(得分:13)

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

对于whichButton,请使用以下其中一项:

DialogInterface.BUTTON_POSITIVE 
DialogInterface.BUTTON_NEGATIVE
DialogInterface.BUTTON_NEUTRAL 

答案 3 :(得分:7)

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

答案 4 :(得分:1)

使用此按钮并设置正面或负面按钮

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

参考这个 http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int,java.lang.CharSequence,android.content.DialogInterface.OnClickListener)

答案 5 :(得分:1)

这是我的简短而又甜蜜的,是Android Developer的优化。它更简洁,使用更少的内存。

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

答案 6 :(得分:0)

您可以使用alertDialog

设置三种按钮
.setPositiveButton("name_of_button",LISTENER);

.setNegativeButton("name_of_button",LISTENER);

.setNeutralButton("name_of_button",LISTENER);

监听器可能在课外或匿名课上,如

.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            alertDialog.cancel();
        }
    });

答案 7 :(得分:0)

Tony Stark here的好帖子,您可以通过添加图标来美化您的对话框。确保您的图片在您的可绘制文件夹中。

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

实施例 enter image description here