动态添加视图并烘烤它

时间:2013-05-21 09:04:03

标签: android android-view toast

我正在动态添加视图。意思是说,最初只有一个EditTextButton AddButton Submit

1.点击Button Add我正在为新布局增加一个EditTextButton Remove。如果继续按下Button Add,它将分别添加膨胀布局。

2.点击Button Submit,它必须为加密布局的EditText值举杯。

我只获得了最后一个EditText Toast,其余EditText的Toast没有显示。怎么做?

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

}

 }

1 个答案:

答案 0 :(得分: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;
    }
}