我正在动态添加视图。意思是说,最初有一个EditText
, Button Add
, Button Submit
。
1.OnClickButton Add
我正在膨胀具有一个EditText
和Button 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;
}
}
}