【问题标题】:AlertDialog with EditText and Three buttons带有 EditText 和三个按钮的 AlertDialog
【发布时间】:2022-01-27 11:10:27
【问题描述】:

所以我有 tis 代码,我正在尝试创建一个带有 EditTet 和三个按钮的 AlertDialog 积极按钮,消极按钮和中性按钮,但它不起作用并且应用程序停止强>

        b5.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("UseCompatLoadingForDrawables")
        @Override
        public void onClick(View view) {
            AlertDialog.Builder boite;
            boite = new AlertDialog.Builder(MainActivity.this);
            boite.setTitle("boite de dialogue");
            boite.setIcon(getDrawable(R.drawable.warning_shield_96px));


           final EditText input = new EditText(MainActivity.this);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            boite.setView(input);

            boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
            boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
            boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
        }
    });

【问题讨论】:

  • boite.show();和 boite.show();和 boite.show(); ????????????

标签: java android android-studio android-layout


【解决方案1】:

不需要多次调用 boite.show(),只需调用一次即可,如下所示:

   b5.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("UseCompatLoadingForDrawables")
            @Override
            public void onClick(View view) {
                AlertDialog.Builder boite;
                boite = new AlertDialog.Builder(MainActivity.this);
                boite.setTitle("boite de dialogue");
                boite.setIcon(getDrawable(R.drawable.warning_shield_96px));


                final EditText input = new EditText(MainActivity.this);
                input.setInputType(InputType.TYPE_CLASS_TEXT);
                boite.setView(input);

                boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.show();
            }
        });

AlertDialog 使用 Builder 模式进行初始化,因此您可以设置不同的方法和按钮以及您喜欢的任何内容,然后当您调用 alertDialog.show() 时,它会使用您在调用之前设置的任何配置来构建对象。

【讨论】:

  • 谢谢!我很感激
猜你喜欢
  • 2012-07-06
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多