问:当用户在上一个屏幕中提交数字时,我正在尝试动态创建textViews。除了不确定如何从视图中获取数据以建立方程式之外,我都能正常使用。我需要获取每个视图的ID,以便我可以编写方程式并对它们进行一些汇总。有什么建议吗?
意图getIntOfMainScrn = getIntent();
int numOfLoads = Integer.parseInt(getIntOfMainScrn.getStringExtra(“ NofL”));;
for(int i = numOfLoads; i> 0; i--){
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams =新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
EditText a =新的EditText(this);
EditText b =新的EditText(this);
EditText p =新的EditText(this);
a.setHint(“ a”); a.setTextSize(100); a.setGravity(Gravity.LEFT);
b.setHint(“ b”); b.setTextSize(20);; b.setGravity(Gravity.CENTER);
p.setHint(“ p”); p.setTextSize(20); p.setGravity(Gravity.RIGHT);
linearLayout.addView(a); linearLayout.addView(b); linearLayout.addView(p);
mainInputLL.addView(linearLayout);
答:将EditTexts存储在数组中。
就像是:
EditText [] editTexts = new EditText [ numOfLoads * 3 ];
int editTextIndex = 0 ;
for (int i = numOfLoads ; i > 0 ; i -) {
EditText a = new EditText (this ); EditText b =新的EditText (this ); EditText p =新的EditText (
此);
editTexts [ editTextIndex ++] = a ; editTexts [ editTextIndex ++] = b ; editTexts [ editTextIndex ++] = p ; }