我已经动态添加了多个EditText和TextViews:
final String[] meses = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
for(int i=0; i<meses.length; i++){
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textview = new TextView(getContext());
ViewGroup.LayoutParams lparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
textview.setLayoutParams(lparams);
textview.setText(meses[i]);
layout.addView(textview);
EditText ed = new EditText(getContext());
ed.setLayoutParams(lparams);
ed.setId(i);
layout.addView(ed);
llMes.addView(layout);
}
但是现在我需要检索每个EditText的值,我该如何检索它,我有这个:
for(int i=0; i < meses.length; i++){
EditText et = null;
et.getId();
pago = quotas - Integer.parseInt(et.getText().toString());
}
还尝试 findViewById(i) ,但也失败了。
感谢您的帮助!
答案 0 :(得分:3)
将EditTexts添加到ArrayList,然后通过索引检索它。
//Arraylist that will contain EditTexts
ArrayList<EditText> list = new ArrayList<>();
// for each EditText add it to the ArrayList
for(int i=0; i < 10; i++){
EditText et = new EditText();
list.add(et);
}
//To retrieve EditText
EditText et = list.get(0);