简体   繁体   English

如何在AlertDialog中添加更改数量的EditText对象

[英]How To Add Changing Number Of EditText Objects in AlertDialog

I'm Developing a game so I want to get player name using an AlertDialog. 我正在开发游戏,所以我想使用AlertDialog获取玩家名称。 But I don't Know certain number of players, it's Variable between 2 to 16! 但是我不知道有多少玩家,它在2到16之间可变!

I've added an spinner to ask about the NumberOfPlayers and a Button to Show AlertDialog then I tried to add certain number of EditText Using for loop. 我添加了一个微调AlertDialog ,询问有关NumberOfPlayers和显示AlertDialog的按钮,然后尝试使用for循环添加一定数量的EditText It doesn't have error but when I run application on phone, I just have Ok and Cancel Buttons. 它没有错误,但是当我在电话上运行应用程序时,我只有“确定”和“取消”按钮。 I couldn't resolve problem and become appreciate if someone help me. 我无法解决问题,如果有人帮助我,我将不胜感激。

This is My AlertDialog 这是我的AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Opponents:");

                LinearLayout layout = new LinearLayout(this);
                layout.setOrientation(LinearLayout.VERTICAL);

                final EditText[] input = new EditText[NumberOfPlayers];
                for (int aux=0;aux==NumberOfPlayers;aux++) {
                    input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
                    layout.addView(input[aux]);
                }

                builder.setView(layout); // this is a set method, not add

                // Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int aux=0;aux==NumberOfPlayers;aux++){
                            //PlayersTXT[aux].setText(input[aux].getText().toString());
                        }
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.show();

Just change your code with below one and you will get dynamic edittext in alert dialog: 只需更改下面的代码,您将在警报对话框中获得动态的edittext:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Opponents:");

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    layout.setOrientation(LinearLayout.VERTICAL);

    final EditText[] input = new EditText[NumberOfPlayers];
    for (int aux=0;aux<NumberOfPlayers;aux++) {
        input[aux] = new EditText(MainActivity.this);
        input[aux].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
        layout.addView(input[aux]);
    }

    builder.setView(layout); // this is a set method, not add

    // Set up the buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            for (int aux=0;aux==NumberOfPlayers;aux++){
                //PlayersTXT[aux].setText(input[aux].getText().toString());
            }
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();

Let me highlight problems in your code are: 让我强调您的代码中的问题是:

  1. you are using "aux==NumberOfPlayers" in "for loop" which is wrong. 您在“ for循环”中使用“ aux == NumberOfPlayers”,这是错误的。 it should be "aux < NumberOfPlayers" 它应该是“ aux <NumberOfPlayers”

  2. you are not initializing edittext in "for loop" such as "input[aux] = new EditText(MainActivity.this);" 您没有在“ for循环”中初始化edittext,例如“ input [aux] = new EditText(MainActivity.this);”

  3. you are not giving height and width for both linear layout as well edittext after initializing such as ".setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));" 初始化后,您不会同时提供线性布局和edittext的高度和宽度,例如“ .setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));“

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

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