Tôi mở nguồn giải pháp của chúng tôi TokenAutoComplete on github. Mỏ đã được thử nghiệm trở lại 2.2. Tôi đã thiết kế mã của mình để cho phép triển khai và tùy chỉnh khá đơn giản. Tôi không chắc chắn nếu điều này khá câu trả lời câu hỏi của bạn, nhưng nó có thể là một điểm khởi đầu tốt hơn so với mã nguồn chip.
Dưới đây là ví dụ triển khai sử dụng thư viện của tôi:
Subclass TokenCompleteTextView
public class ContactsCompletionView extends TokenCompleteTextView {
public ContactsCompletionView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View getViewForObject(Object object) {
Person p = (Person)object;
LayoutInflater l = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)l.inflate(R.layout.contact_token, (ViewGroup)ContactsCompletionView.this.getParent(), false);
((TextView)view.findViewById(R.id.name)).setText(p.getEmail());
return view;
}
@Override
protected Object defaultObject(String completionText) {
//Stupid simple example of guessing if we have an email or not
int index = completionText.indexOf('@');
if (index == -1) {
return new Person(completionText, completionText.replace(" ", "") + "@example.com");
} else {
return new Person(completionText.substring(0, index), completionText);
}
}
}
đang Layout cho contact_token (bạn có thể sử dụng bất kỳ loại bố trí tại đây hoặc có thể ném một ImageView nhập nếu bạn muốn hình ảnh trong thẻ)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/token_background"
android:padding="5dp"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
Mã backgound drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffafafaf" />
<corners
android:topLeftRadius="5dp"
android:bottomLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp" />
</shape>
Person mã đối tượng
public class Person implements Serializable {
private String name;
private String email;
public Person(String n, String e) { name = n; email = e; }
public String getName() { return name; }
public String getEmail() { return email; }
@Override
public String toString() { return name; }
}
hoạt động Mẫu
public class TokenActivity extends Activity {
ContactsCompletionView completionView;
Person[] people;
ArrayAdapter<Person> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
people = new Person[]{
new Person("Marshall Weir", "[email protected]"),
new Person("Margaret Smith", "[email protected]"),
new Person("Max Jordan", "[email protected]"),
new Person("Meg Peterson", "[email protected]"),
new Person("Amanda Johnson", "[email protected]"),
new Person("Terry Anderson", "[email protected]")
};
adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people);
completionView = (ContactsCompletionView)findViewById(R.id.searchView);
completionView.setAdapter(adapter);
completionView.setPrefix("To: ");
}
}
đang Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.tokenautocomplete.ContactsCompletionView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
hãy cố gắng tìm một từ khác nhau cho 'chip'. Tôi không có ý tưởng gì về một con chip, khác với cái tên thích hợp cho một "khoai tây chiên" hoặc tên Mỹ cho khoai tây sắc nét. – Simon
Hình nền của chuỗi có thể kéo vào trường được gọi là chip. bất kỳ cách nào tôi đã thay đổi văn bản chip đó thành bong bóng. Tôi nghĩ rằng hầu hết các nhà phát triển Android sẽ biết về chip hoặc buble với chế độ xem toàn văn nhiều lần. nếu có bất kỳ ý tưởng xin vui lòng cho tôi biết về giải pháp .. @ Simon – AndroidDev