从动态生成的Edittext获取输入值

时间:2015-03-03 07:27:00

标签: android android-layout android-edittext

我必须创建n EditText,其中n是用户输入。

我可以使用for循环创建它。

TableLayout tbl=(TableLayout)findViewById(R.id.TableLayout1);
    //table row
    for (int i = 0; i < 5; i++) {
        TableRow tr = new TableRow(this);
        TableLayout.LayoutParams tableRowParams=
                new TableLayout.LayoutParams
                (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
        //for set margin
        tableRowParams.setMargins(0, 10, 0, 0);
        tr.setLayoutParams(tableRowParams);
        tr.setGravity(Gravity.CENTER_HORIZONTAL);
        //text view
        TextView tv=new TextView(this);
        tv.setText("Field "+(i+1));
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#0070C0"));
        tv.setTextSize(26);
        tv.setLayoutParams(new TableRow.LayoutParams(100, TableRow.LayoutParams.WRAP_CONTENT));
        //add textview
        tr.addView(tv);
        //set layout params of edittext
        TableRow.LayoutParams etParams=
                new TableRow.LayoutParams
                (120,30);
        etParams.setMargins(10, 0, 0, 0);

        EditText et=new EditText(this);
        et.setLayoutParams(etParams);
        et.setBackgroundResource(R.drawable.bg_grey);
        et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        tr.addView(et);
        tbl.addView(tr, tableRowParams);
    }

我有布局

enter image description here

让我知道如何从动态创建的EditTexts中检索数据。

3 个答案:

答案 0 :(得分:4)

添加editText

//list to store edittexts
List<EditText> etList = new ArrayList<EditText>();
TableLayout tbl=(TableLayout)findViewById(R.id.table);
for(int i=1;i<=5;i++){
    TableRow tr = new TableRow(CurrentActivity.this);  
    TableLayout.LayoutParams tableRowParams=
        new TableLayout.LayoutParams
        (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);       
    EditText et=new EditText(CurrentActivity.this);
    et.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER);
    et.setId(Id+i);//string+i
    //add et to table row
    tr.addView(et);
    //add table row to table            
    tbl.addView(tr, tableRowParams);
    //add edittext to list
    etList.add(et);
}

获取值

for (EditText et : etList) {
    System.out.println(et.getText().toString());
}

答案 1 :(得分:1)

在创建EditTexts时,应将它们存储在数组或列表

for (int i = 0; i < 5; i++) {
    EditText myEditText = new EditText();
    myEditTextList.add(myEditText);
}

在此之后,您可以遍历列表并从EditTexts获取字符串:

for (EditText editText : myEditTextList) {
    String text = editText.getText().toString();
    Log.d(TAG, text);
}

答案 2 :(得分:0)

在布局xlm中创建LinearLayout

在Activity onCreate方法中找到那个布局

containerLayout = (LinearLayout)findViewById(R.id.LinearLayout1);

//now create loop
for(int i=0; i<=arraysize; i++){

EditText editText+String.valueof(i) = new EditText(getBaseContext());
    containerLayout.addView(editText+String.valueof(i));
      editText.setGravity(Gravity.Left);
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
      layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
      layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
       // layoutParams.setMargins(23, 34, 0, 0);
     // RelativeLayout.LayoutParams()
       editText.setLayoutParams(layoutParams);
       //if you want to identify the created editTexts, set a tag, like below
     editText+String.valueof(i).setTag("EditText" + i);

}
相关问题