简体   繁体   English

带有 EditText 和三个按钮的 AlertDialog

[英]AlertDialog with EditText and Three buttons

So i have tis code and i'm trying to create a AlertDialog with an EditTet and Three buttons the positive one, the négative one and the neutral one, but it doesn't work and the application stops所以我有 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();
        }
    });

There is no need to call boite.show() several times, just call it once like below:无需多次调用 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 uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call. AlertDialog 使用 Builder 模式进行初始化,因此您可以设置不同的方法和按钮以及您喜欢的任何内容,然后当您调用 alertDialog.show() 时,它会使用您在调用之前设置的任何配置构建 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM