eclipse AlertDialog setButton 已被弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:36:09  来源:igfitidea点击:

AlertDialog setButton was deprecated

androideclipse

提问by Ataberk

I us this code in my Eclipse Android project

我在我的 Eclipse Android 项目中使用了这段代码

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

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

        }
});

But Eclipse is saying:

但是 Eclipse 说:

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

此方法在 API 级别 3 中已弃用。将 setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) 与 BUTTON_POSITIVE 一起使用

回答by Shahzad Afridi

Java

爪哇

AlertDialog alert = new AlertDialog.Builder(this).create();
            alert.setTitle("Error");
            alert.setMessage("Sorry, your device doesn't support flash light!");
            alert.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){

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

alert.show();

Kotlin

科特林

    var alert: AlertDialog = AlertDialog.Builder(this).create()
    alert.setTitle("Error")
    alert.setMessage("Sorry, your device doesn't support flash light!")
    alert.setButton(Dialog.BUTTON_POSITIVE, "OK", DialogInterface.OnClickListener {
            //do your own idea.
            dialog, which -> finish() })
    alert.show()

回答by dumazy

To make sure your dialogs match the design guidelines, the API now has 3 types of buttons for an AlertDialog: BUTTON_POSITIVE, BUTTON_NEUTRAL and BUTTON_NEGATIVE. This also provides correct positions for right to left support.

为确保您的对话框符合设计准则,API 现在为 AlertDialog 提供 3 种类型的按钮:BUTTON_POSITIVE、BUTTON_NEUTRAL 和 BUTTON_NEGATIVE。这也为从右到左的支撑提供了正确的位置。

I would advice to create your AlertDialogwith the builder pattern

我建议AlertDialog使用构建器模式创建您的

AlertDialog.Builder builder = new AlertDialog.Builder(myContext);//Context parameter
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
            //do stuff
    }
});
builder.setMessage("some message");
AlertDialog alertDialog = builder.create();

More information on the AlertDialog.Buildercan be found in the API reference

AlertDialog.Builder可以在API 参考中找到有关 的更多信息

回答by Kabilan

alertDialog=new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setTitle("Hello");
alertDialog.setMessage("Hai");
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "hai", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "Thanks", Toast.LENGTH_SHORT).show();
    }
});

alertDialog.show();

回答by Manish Raj

You can use this but better use a AlertDialog.Builder next time.

您可以使用它,但下次最好使用 AlertDialog.Builder。

   alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
            "OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

回答by Douglas Fornaro

You can use this:

你可以使用这个:

alertDialog.setPositiveButton(R.string.yes, yesListener);
alertDialog.setNegativeButton(R.string.no, noListener);

回答by Inzimam Tariq IT

setButton();method accept three parameters

setButton();方法接受三个参数

  1. Button Type
  2. Button Text
  3. Message/OnClickListener // Message or clickListener
  1. 按钮类型
  2. 按钮文字
  3. 消息/点击监听器 // Message or clickListener

Provide three params and it will work OK. If you do not need 3rd parameter you can pass null as

提供三个参数,它会正常工作。如果您不需要第三个参数,您可以将 null 作为

alertDialog.setButton(BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);

OR

或者

alertDialog.setButton(BUTTON_POSITIVE, "OK", (Message) null);

Ifyou do not want to pass three parameters Use AlertDialog.builder's method setPositiveButton();.

如果不想传递三个参数,请使用AlertDialog.builder的方法setPositiveButton();