获取android中循环中膨胀的所有edittext值
你好,我是充气xml文件像获取android中循环中膨胀的所有edittext值,android,layout-inflater,Android,Layout Inflater,你好,我是充气xml文件像 List<EditText> allEds3 = new ArrayList<EditText>(); LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (int hh1 = 57; hh1 <=
List<EditText> allEds3 = new ArrayList<EditText>();
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int hh1 = 57; hh1 <= 76; hh1++) {
try {
View view = inflater.inflate(R.layout.form6, null);
form6_linear.addView(view);
lbl1 = (TextView) view.findViewById(R.id.lbl1);
lbl2 = (TextView) view.findViewById(R.id.lbl2);
txt1 = (TextView) view.findViewById(R.id.txt1);
txt1.setId(hh1);
txt1.setText(txt_array.get(hh1));
txt1.setOnClickListener(onclicklistener);
_txt2 = (EditText) view.findViewById(R.id.txt2);
lbl1.setText(lbl_array.get(hh1));
lbl2.setText(lbl_array.get(hh1 + 1));
_txt2.setText(txt_array.get(hh1 + 1));
allEds3.add(_txt2);
hh1++;
nn1++;
} catch (Exception e) {
// TODO: handle exception
}
}
List allEds3=new ArrayList();
LayoutFlater充气器=充气器=(LayoutFlater)getApplicationContext()
.getSystemService(上下文布局\充气机\服务);
对于(int hh1=57;hh1,如果在活动类中将_txt2定义为一个字段,则只需单击按钮即可获得它:
// in onCreate block of your activity
Button button = (Button) findViewById(R.id.button_to_click);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// here you can do anything with _txt2 which is just executed when button is clicked
}
}
编辑:我没有正确理解您的问题,因此我应该添加以下内容:
您可以为创建的任何EditText设置ID。然后在需要时通过findViewById()查找它们。或者您可以存储所有EditText在一个EditText数组中,稍后再获取它们。但我有21个EditText。现在如何获取用户输入到onclick方法中的所有EditText字符串。我是onclick Listener。当用户当时单击按钮时,我想获取所有EditText的文本,而不是在创建时。那么,我将用于将所有值存储到数组中的代码放在哪里?@hardikjo好的,我编辑了我的答案,用一个数组来存储所有21个编辑文本,然后点击按钮。lbl_数组的成员数在57到76之间。但是strings2没有被调用。我认为只有一个成员。有什么问题吗?我认为最好保持我的答案为“已接受”,作为您解决的第一个问题。然后问一个新问题以继续解决下一个问题:)记录数据库以在错误发生时找出其值。您在txt_数组中没有足够的成员,或者位置57..76之间的字符串2。lbl_数组的成员数在57到76之间。但是strings2并不是因为allEds3只有一个成员,我认为。所以在第一个“for”循环之后记录allEds3的大小,看看它有多大。09-08 10:11:22.389:i/allEds3的大小------(1767):10它在for循环之后显示10。这是因为在第一个循环中,当你膨胀并创建EditText时,你增加了循环中的循环计数器!!!删除hh1++;在循环结束时,因为您已经在循环本身中声明了它,所以循环计数器在每次执行中增加两次,因此它循环10次而不是20次,并且只创建10个视图而不是20个视图。
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_continue1:
final String[] strings2 = new String[allEds3.size()];
for (int i = 0; i < allEds3.size(); i++) {
strings2[i] = allEds3.get(i).getText().toString();
}
int ii = 0;
for (int db = 57; db <= 76; db++) {
try {
j.remove(txt_array.get(db));
j.put(txt_array.get(db), strings2[ii]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii++;
}
}
}
// in onCreate block of your activity
Button button = (Button) findViewById(R.id.button_to_click);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// here you can do anything with _txt2 which is just executed when button is clicked
}