나는 Splitwise에서 우리의 사용을 위해 함께 TokenAutoComplete on github 넣었습니다. Android SDK에서 이와 같은 것을 찾을 수 없으므로 직접 만들었습니다.
내 컨트롤의 동작이 예상과 일치하지 않는 유일한 위치는 가장 최근에 완료된 토큰을 삭제하면 다시 단어로 바뀝니다. 다른 모든 토큰은 완전히 삭제됩니다.
<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>
(여기 레이아웃의 어떤 종류를 사용하거나 토큰의 이미지를 원하는 경우 이미지 뷰를 던져 수) contact_token에 대한
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);
}
}
}
레이아웃 코드 : 여기
은 기본 예제 토큰 backgound에 그릴 수
<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 객체 코드
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; }
}
샘플 활동
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);
}
}
레이아웃 코드
<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>
내가 너무 늦게 답장을하기 때문에 그래서 죄송합니다. 감사합니다. @cimnine. 하지만 ICS에서만 사용 가능한 것으로 보입니다. 나는 그것이 RecipientEditTextView를 사용하는 것을 본다. Android SDK에서 볼 수 없습니다. 연구 후 나는 이것을 발견했다 : https://android.googlesource.com/platform/frameworks/ex/+/refs/heads/master/chips. ICS에서만 사용 가능합니다.(아직 귀하의 의견을 따르려고 시도하지 않았습니다). 내 안드로이드 버전 3.0입니다. – gZerone
죄송하지만 나는 더 좋은 대답을 드릴 수 없습니다. 나는 한동안 해결책을 찾았고 뭔가를 찾지도 못했다. 아무리해도 기존 코드를 Android 3.0으로 다시 이식하는 것을 막을 수는 없지만 직접 솔루션을 생각해 보면 더 안전하고 신속하게 진행할 수 있습니다. 왜 오픈 소스로 만들지 않겠습니까? – cimnine
안녕하세요. 내 능력에서 벗어난 것 같아. 내 프로젝트에 다른 방법을 사용하고 있습니다. 나는 더 이상 Edittext를 사용하지 않았다. 그리고 저는 그것이 마치 해커처럼 생각합니다. 그래서. 나는 공개하지 말아야한다. 하지만이 링크를 찾았습니다 : http://ballardhack.wordpress.com/2011/07/25/customizing-the-android-edittext-behavior-with-spans/ 나는 그것을 아주 잘 생각하고 다른 사람들을 도울 것입니다. – gZerone