简体   繁体   English

将文本从动态创建的edittext放入按钮android java

[英]getting text from dynamically created edittext into a button android java

I have created dynamically edittext on my android application like below: 我已经在我的android应用程序上动态创建了edittext,如下所示:

protected void onCreate(Bundle savedInstanceState) {

RelativeLayout layout = (RelativeLayout) findViewById(R.id.linearLayout1);
        final EditText[] Et = new EditText[10];
        int prevTextViewId = 0;
        int add = 0;
        int add1 = 0;
        for (int a = 0; a < Arr.length; a++) {

            Et[a] = new EditText(this);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                    (int) LayoutParams.WRAP_CONTENT,
                    (int) LayoutParams.WRAP_CONTENT);
            param.leftMargin = 25;
            param.topMargin = (a + 1) * (100 + add1);
            Et[a].setPadding(5, 23, 5, 5);
            Et[a].setLayoutParams(param);
            Et[a].setSingleLine(false);
            Et[a].setHorizontalScrollBarEnabled(false);
            Et[a].setWidth(280);
            Et[a].getText().toString();
            layout.addView(Et[a]);
            add = add + 25;
            add1 = add1 + 15;
        }
}

And I want to get text from dynamically created edittext into a button like below: 我想将动态创建的edittext中的文本放入如下所示的按钮中:

btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
        btn_doneAnswer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                   for (int a = 0; a < Arr.length; a++) {
                        Et[a] = new EditText(this);
                        String text += Et[a].getText().toString();
                    }
                    Bundle bundle = new Bundle();
                    Intent i_backToAddItem = new Intent(
                            AnswerActivityMultiText.this,
                            QuestionActivityDynamic.class);
                    bundle.putString("text", text);
                    bundle.putString("MultiQues", MultiQues);
                    i_backToAddItem.putExtras(bundle);
                    startActivity(i_backToAddItem);

            }
        });

But I am getting error below in the button onclick method at this line Et[a] = new EditText(this); 但是我在下面这行的按钮onclick方法中遇到错误Et[a] = new EditText(this); :

The constructor EditText(new View.OnClickListener(){}) is undefined

another below error at this line += in the button onclick method: 按钮onclick方法中此行+=另一个错误:

Syntax error on token "+=", = expected

Kindly suggest me how I am getting text from dynamically created edittext into a button. 请建议我如何将动态创建的edittext中的文本输入按钮。

Waiting for reply. 等待回复。

Thanks 谢谢

you dont have to initialize the EditText again in the onClick. 您不必在onClick中再次初始化EditText。 You should directly use the EditText object to get the text. 您应该直接使用EditText对象获取文本。 So the line 所以线

Et[a] = new EditText(this);

is not required at all! 完全不需要!

And for the error with '+=' operator, you are declaring the string and using the operator(+=) in one line. 对于“ + =”运算符的错误,您需要声明字符串并在一行中使用operator(+ =)。 you have to split up the the declaration part out of the for loop and then use '+='. 您必须将声明部分从for循环中分离出来,然后使用'+ ='。 Here's the complete code : 这是完整的代码:

btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
    btn_doneAnswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                String text;
               for (int a = 0; a < Arr.length; a++) {
                    text += Et[a].getText().toString();
                }
                Bundle bundle = new Bundle();
                Intent i_backToAddItem = new Intent(
                        AnswerActivityMultiText.this,
                        QuestionActivityDynamic.class);
                bundle.putString("text", text);
                bundle.putString("MultiQues", MultiQues);
                i_backToAddItem.putExtras(bundle);
                startActivity(i_backToAddItem);

        }
    });

Your problem is in this lines: 您的问题在于以下几行:

for (int a = 0; a < Arr.length; a++) {
    Et[a] = new EditText(this);
    String text += Et[a].getText().toString();
}

In this line: 在这一行:

Et[a] = new EditText(this);

this is the onClickListener object, you must have class.this (where class is the class name of file that this code is created). 是onClickListener对象,您必须具有class.this(其中class是创建此代码的文件的类名)。

In this other line: 在另一行中:

String text += Et[a].getText().toString();

You always create a new String, and instead of created, you make an add. 您总是创建一个新的String,而不是创建一个,而是进行添加。 It is a Java concept fail. 这是Java概念的失败。

You must do this: 您必须这样做:

String text = "";
for (int a = 0; a < Arr.length; a++) {
    Et[a] = new EditText(class_name.this);
    text += Et[a].getText().toString();
}

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

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