新手上路,没找到动态添加删除多个控件,捣鼓了个,做错的地方麻烦大家说下
activity_main.xml:
<Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myclick" android:text="add" /> <!-- 动态添加处 --> <LinearLayout android:id="@+id/addlayout" android:layout_width="match_parent" android:layout_above="@+id/surebutton" android:layout_height="match_parent" android:layout_below="@+id/add" android:orientation="vertical" > </LinearLayout>
<Button android:id="@+id/surebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:onClick="myclick" android:text="确定" />
<!-- 添加对象是一个布局文件 -->
addlayout.xml:
<RelativeLayout xmlns:android="http://schemas./apk/res/android" xmlns:tools="http://schemas./tools" android:layout_width="match_parent" android:layout_height="match_parent" >
<Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_alignParentRight="true" android:background="@drawable/delete" android:onClick="myclick" />
<Button android:id="@+id/unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/delete" android:layout_toLeftOf="@+id/delete" android:onClick="myclick" android:text="无" />
<EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/delete" android:layout_toLeftOf="@+id/unit" />
</RelativeLayout>
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry;
import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener { LinearLayout ll;
//我用List添加多个对象后不能正常删除控件,如果有人知道怎么做麻烦告诉我一下,新手小白一枚。。。 Map<Integer, Button> deletebutton; Map<Integer, Button> unitbutton; Map<Integer, EditText> edittext; Map<Integer, View> vi;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll = (LinearLayout) findViewById(R.id.addlayout); deletebutton = new HashMap<Integer, Button>(); unitbutton = new HashMap<Integer, Button>(); edittext = new HashMap<Integer, EditText>(); vi = new HashMap<Integer, View>();
} //获取添加对象 public View getPartView() { return LayoutInflater.from(this).inflate(R.layout.addlayout, null); }
int tag = 0;//循环
public void myclick(View v) { switch (v.getId()) { case R.id.add: View view = getPartView(); // 添加右边删除按钮 Button button = (Button) view.findViewById(R.id.delete); button.setBackgroundResource(R.drawable.delete);
//之所以用tag不用id的原因是监听的时候id和别的控件重复,别的控件能响应到设置的id button.setTag(tag); button.setOnClickListener(this); deletebutton.put(tag, button); // 中间按钮,想换成Spinner,现在没做,可忽略 Button changebutton = (Button) view.findViewById(R.id.unit); changebutton.setOnClickListener(this); unitbutton.put(tag, changebutton); // 添加输入框 EditText edit = (EditText) view.findViewById(R.id.edittext); edittext.put(tag, edit); ll.addView(view);//将view对象添加到布局文件 vi.put(tag, view); System.out.println("addtag:" + tag); tag++; break; case R.id.surebutton: Iterator iter = edittext.entrySet().iterator(); while (iter.hasNext()) { Entry entry = (Entry) iter.next(); System.out.println(entry.getKey() + ":" + ((EditText) entry.getValue()).getText().toString()); } default: break; } }
@Override public void onClick(View v) {
//hashmap的循环遍历方式 Iterator iter = deletebutton.entrySet().iterator(); while (iter.hasNext()) { Entry entry = (Entry) iter.next(); if (v.getTag().equals(entry.getKey())) { ll.removeView(vi.get(v.getTag())); deletebutton.remove(v.getTag()); unitbutton.remove(v.getTag()); edittext.remove(v.getTag()); break; } }
} }
|