我们先来看一下实现效果图
联系人输入框里有标签式的View,把不同的联系人分割开,要重写EditText吗?
其实不必的,时间久了,你就会发现,很多你看到的View并不是你想的那这样
我们可以用LinearLayout(其他也可以,看个人爱好)来做这个输入框,然后addView联系人标签,最后添加一个EditText
1、这是输入联系人布局
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#333" >
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@null"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="@drawable/back"
android:textColor="#f0f0f0" />
<LinearLayout
android:id="@+id/contacts_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/contacts"
android:layout_toRightOf="@+id/back"
android:background="@drawable/circle"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="@+id/contacts_input"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@null"
android:minWidth="10dp"
android:singleLine="true" />
</LinearLayout>
<ImageButton
android:id="@+id/contacts"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@null"
android:src="@drawable/contacts" />
</RelativeLayout>
2、这是添加联系人标签代码
private void addContacts(String name) {
mContactsLayout.addView(initContactsItemView(name),
mContactsLayout.getChildCount() - 1);
}
private View initContactsItemView(String name) {
final View view = LayoutInflater.from(this).inflate(R.layout.item_contacts,
null);
TextView textView = (TextView) view.findViewById(R.id.name);
ImageButton button = (ImageButton)view.findViewById(R.id.delete);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mContactsLayout.removeView(view);
}
});
textView.setText(name);
return view;
}
差不多就是这些了,具体看代码吧