2015-03-03 86 views
2

我必須創建n EditText其中n是用戶輸入。從動態生成的Edittext獲取輸入值

我可以使用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

讓我知道了如何獲取從dymamically創建EditTexts數據。

+0

n次創建循環,在循環中,您可以訪問的EditText輸入的所有文字... – 2015-03-03 07:29:35

+0

如何設置和獲取IDS? – 2015-03-03 07:32:39

回答

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()); 
} 
+0

需要注意的是:列表 etList = new ArrayList (); 和etList.add(et);謝謝@MadDeveloper :) – kiturk3 2015-11-20 11:14:20

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); 
} 
0

在你的layout xlm中創建LinearLayout

in Activity onCreate method find that layout

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); 

}