令牌EDITTEXT(场)的Android [英] Token Edittext (Field) android
问题描述
由于某种原因,我需要像在此图像中红色部分EDITTEXT:
Because some reason I need a Edittext like the red part in this image :
每当用户preSS删除键盘按钮,的EditText将删除一个令牌而不是一个字。 所以,我的问题是: 我们有喜欢它的存在控制? 如果还是不行,你知道如何定制一个。 注:我不需要用同样的100%。现在,我正在考虑使用TextWatcher或setKeyListener方法删除功能。
Whenever the user press delete button on keyboard, the edittext will delete one token instead of one word. So, my question are : Do we have an exist control like it? or If not, do you know how can customize one. Note : I don't need it the same 100%. Right now, I am thinking about using a TextWatcher or setKeyListener method for delete feature.
感谢你这么多的帮助。很遗憾,因为我的英语不是很好。
Thank you so much for any help. And sorry because my English is not really well.
推荐答案
我已经把 TokenAutoComplete在github 一>我们在Splitwise使用。我找不到这样的事在Android SDK,所以我做了我自己。
I've put together TokenAutoComplete on github for our use at Splitwise. I couldn't find anything like this in the Android SDK, so I made my own.
在我的控制行为将不匹配您的期望只有一个地方是,当你删除最最近完成的标记,它会变成这个词了。所有其他标记得到彻底删除。
The only place where the behavior of my control will not match your expectations is that when you delete the most-recently completed token, it turns into the word again. All other tokens get completely deleted.
下面是一个基本的例子:
Here's a basic example:
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);
}
}
}
布局$ C $下contact_token(你可以使用任何种类的布局,或在这里如果你想在令牌图像可以抛出的ImageView中)
Layout code for contact_token (you can use any kind of layout here or could throw an ImageView in if you want images in the token)
<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>
令牌backgound绘制
Token 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对象code
Person object code
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", "marshall@example.com"),
new Person("Margaret Smith", "margaret@example.com"),
new Person("Max Jordan", "max@example.com"),
new Person("Meg Peterson", "meg@example.com"),
new Person("Amanda Johnson", "amanda@example.com"),
new Person("Terry Anderson", "terry@example.com")
};
adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people);
completionView = (ContactsCompletionView)findViewById(R.id.searchView);
completionView.setAdapter(adapter);
}
}
布局code
Layout code
<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>
这篇关于令牌EDITTEXT(场)的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!