0

您好,我正在膨胀 xml 文件,例如..

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
        }
    }

现在我的主 xml 文件中有一个按钮,单击该按钮我必须获取上面这个_txt2的所有值。

请帮忙。

编辑:

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
    }
}

点击()

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++;
        }
    }

}

错误:- ArrayIndexOutOfBounds 在

j.put(txt_array.get(db), strings2[ii]);

4

1 回答 1

1

如果 _txt2 被定义为您的活动类中的一个字段,那么您可以简单地在按钮的 onClick 上获取它:

// 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(s) 存储在 EditText 数组中,然后再获取它们。

于 2012-09-08T09:10:56.427 回答