我已经问过问题(http://stackoverflow.com/questions/12663443/add-delete-option-with-dynamically-generated-edittext)。我成功地实现了答案,但是这段代码的问题是 textEdit、微调器和删除按钮一起没有从视图中删除。它发生了,但我必须单击三个按钮才能发生这种情况.. 请通过我的代码。
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
final EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(MY_BUTTON);
System.out.println(MY_BUTTON);
//newPass.setOnClickListener(this);
rAlign.addView(newPass);
addSpinner();//Code to add spinner
Button btnRemoveOld = new Button(getApplicationContext());
btnRemoveOld.setId(MY_BUTTON); // arbitrary number
rAlign.addView(btnRemoveOld);
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int idf =MY_BUTTON -1;
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(idf));
allEds.remove(newPass);
}
});
MY_BUTTON ++;
}
});