0

我想创建一个包含许多字段的行,如下所示:

我想这个

Semper que eu clicar em "Add line fields" deve-se criar uma linha abaixo da outra com os 3 campos。

目前我的代码只插入一个EditText。代码:

final List<EditText> newFields = new ArrayList<EditText>();
    final LinearLayout ll = (LinearLayout)findViewById(R.id.llAddNewFields);
    Button btnCreateField = (Button)findViewById(R.id.btCreateField);
    btnCreateField.setOnClickListener( new View.OnClickListener() {
        public void onClick(View v) {               
            MY_COUNT++;
            EditText et = CriaeditText(String.valueOf(MY_COUNT));
            newFields.add(et);
            ll.addView(telefones.get(newFields.size()-1), new ViewGroup.LayoutParams(300,LayoutParams.WRAP_CONTENT));               
        }
    });

方法创建EditText:

private EditText CriaeditText(String hint) {
    EditText editText = new EditText(this);
    editText.setId(Integer.valueOf(hint));
    editText.setHint(hint);
    editText.setTag(hint);
    return editText;
}

只获取一个EditText的值:

 private List getValuesDynamicField(LinearLayout l, List<EditText> li){
    String[] arrValues = null;
    List data = new ArrayList<String>();
    for (int i = 0; i < l.getChildCount(); i++)
    {
        if (l.getChildAt(i).getVisibility() != View.GONE)
        {
            String p = li.get(i).getText().toString();
            if (!p.equals(""))
            {
                data.add(p);
            }
        }
    }
    return data;                
}

如何调整我的代码以根据需要插入 3 个字段?

4

1 回答 1

1

这是使用三个编辑文本动态添加行的代码,

首先,将表格布局添加到布局 xml 文件中。添加将其 id 设置为“tablelayout”。然后在代码中,

 TableLayout tl = (TableLayout)findViewById(R.id.tablelayout);

 TableRow tr = new TableRow(YourActivity.this);

 EditText edit1=new EditText(YourActivity.this);
 edit1.setPadding(5, 5, 5, 5);
 EditText edit2=new EditText(YourActivity.this);
 edit2.setPadding(5, 5, 5, 5);
 EditText edit3=new EditText(YourActivity.this);
 edit3.setPadding(5, 5, 5, 5);

 tr.addView(edit1);
 tr.addView(edit2);
 tr.addView(edit3);

 tl.addView(tr, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    }
于 2013-05-27T04:37:33.453 回答