0

我正在动态添加视图。意思是说,最初有一个EditText, Button Add, Button Submit

1.OnClickButton Add我正在膨胀具有一个EditTextButton Remove. 如果Button Add按住不放,它将分别添加膨胀布局。

2.OnClickButton Submit它必须 Toast InflatedEditText布局的值。

我只得到最后一个EditText吐司,其余的吐司EditText没有显示。这个怎么做?

public class MainActivity extends Activity implements OnClickListener {
Button add, submit;
ArrayList<View> viewList;
LinearLayout lin_layout;
EditText et1, et2;
String s1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1 = (EditText) findViewById(R.id.editText1);
    add = (Button) findViewById(R.id.button_add);
    submit = (Button) findViewById(R.id.bSubmit);
    lin_layout = (LinearLayout) findViewById(R.id.linearLayout_view);
    viewList = new ArrayList<View>();
    add.setOnClickListener(this);
    submit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_add:
        if (et1.length() == 0) {
            Toast.makeText(getApplicationContext(),
                    "Filed cant be left empty", 0).show();
        } else {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(
                    R.layout.inflate_layout, null);
            et2 = (EditText) addView.findViewById(R.id.edit);

            viewList.add(addView);
            lin_layout.addView(addView);

            Button remove = (Button) addView
                    .findViewById(R.id.button_remove);
            remove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    lin_layout.removeView((View) v.getParent());
                    viewList.remove((View) v.getParent());

                }
            });

        }
        break;
    case R.id.bSubmit:
        s1 = et2.getText().toString();
        Toast.makeText(getApplicationContext(), "items: " + s1,Toast.LENGTH_SHORT).show();
        break;
    }

}

 }
4

1 回答 1

1

解决方案非常简单,只需在本地创建 editText 的实例,它就可以工作了:) 现在,一切正常,但是当您使用引用旧的 edittext 时会出现问题

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_add:
        if (et1.length() == 0) {
            Toast.makeText(getApplicationContext(), "Filed cant be left empty", 0).show();
        } else {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.inflate_layout, null);
            EditText et2 = (EditText) addView.findViewById(R.id.edit);
            viewList.add(addView);
            lin_layout.addView(addView);
            Button remove = (Button) addView.findViewById(R.id.button_remove);
            remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lin_layout.removeView((View) v.getParent());
                    viewList.remove((View) v.getParent());
                }
            });
        }
        break;
    case R.id.bSubmit:
        s1 = et2.getText().toString();
        Toast.makeText(getApplicationContext(), "items: " + s1, Toast.LENGTH_SHORT).show();
        break;
    }
}
于 2013-05-22T18:46:26.633 回答