简体   繁体   English

如何在android中动态添加edittext?

[英]How to add edittext dynamically in android?

How to add edittext dynamically in adapter?如何在适配器中动态添加edittext?

i have an adapter from which i am creating edittext for user to input data dynamically.我有一个适配器,我正在从中创建编辑文本供用户动态输入数据。

i am storing all the created edit text from Json received from server in a list.我将从服务器收到的所有从 Json 创建的编辑文本存储在一个列表中。 Now i even have to provide an extra edit text for user to add dynamic/custom data on button click?现在我什至必须为用户提供额外的编辑文本以在按钮单击时添加动态/自定义数据?

How do i do it?我该怎么做?

    ArrayList<EditText> list = new ArrayList<>();
    list.add(holder.editText);

I will first get the title/hint of edit text from user using an alert box like this:我将首先使用这样的警告框从用户那里获取编辑文本的标题/提示:

addField.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogStyle);
            alertDialog.setMessage("");


            final EditText input = new EditText(context);
            input.setGravity(Gravity.CENTER);
            input.setHint("Enter Title for custom field");

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setIcon(R.drawable.logo);

            alertDialog.setPositiveButton("Set",
                    new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which)
                        {
                            list.add(holder.editText); //**App Crashes while doing this**
                        }
                    });

            alertDialog.setCancelable(false).show();
        }
    });

The edittext is not added to list and the app crashes on clicking on SET, of alertBox编辑文本未添加到列表中,并且应用程序在单击警报框的 SET 时崩溃

I think you must provide at least one editText for focus in XML.我认为您必须至少提供一个用于 XML 焦点的 editText。 Then in a java, you can create editTexts and programmatically add params etc. This source code worked for me:然后在 java 中,您可以创建 editTexts 并以编程方式添加参数等。此源代码对我有用:

EditText editText = new EditText(context);
            editText.setLayoutParams(layoutParamsForEdittext);
            editText.setBackground(context.getResources().getDrawable(R.drawable.bg_rounded_grey_border));
            editText.setId(surveyAnswers.getId());
            editText.setPadding(8, 8, 8, 8);
            editText.setScrollContainer(true);
            editText.setTextSize(16);
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(10)});
            editText.setGravity(Gravity.TOP);
            editText.setHintTextColor(context.getResources().getColor(R.color.colorTextHint));
            editText.setHint(surveyAnswers.getText());
          
            linearLayoutShelfShareAnswer.addView(editText);

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

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