从动态生成的 Edittext 中获取输入值
Get Input value from Edittext which generated dynamically
我必须创建 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);
}
我得到了布局
让我知道如何从动态创建的 EditText 中检索数据。
在创建 EditText 时,您应该将它们存储在数组或列表中
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);
}
在布局 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);
}
添加编辑文本
//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());
}
我必须创建 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);
}
我得到了布局
让我知道如何从动态创建的 EditText 中检索数据。
在创建 EditText 时,您应该将它们存储在数组或列表中
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);
}
在布局 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);
}
添加编辑文本
//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());
}