因为某种原因,我需要一个EDITTEXT这样的图像中的红色部分:令牌EDITTEXT(场)的Android
当用户按下删除键盘上的按键,EditText上会删除一个令牌,而不是一个词。 所以,我的问题是: 我们有一个像它这样的存在控制吗? 或者如果没有,你知道如何定制一个。 注:我不需要100%。现在,我正在考虑使用TextWatcher或setKeyListener方法来删除功能。
非常感谢您的帮助。抱歉,因为我的英语不太好。
因为某种原因,我需要一个EDITTEXT这样的图像中的红色部分:令牌EDITTEXT(场)的Android
当用户按下删除键盘上的按键,EditText上会删除一个令牌,而不是一个词。 所以,我的问题是: 我们有一个像它这样的存在控制吗? 或者如果没有,你知道如何定制一个。 注:我不需要100%。现在,我正在考虑使用TextWatcher或setKeyListener方法来删除功能。
非常感谢您的帮助。抱歉,因为我的英语不太好。
Android AOSP电子邮件客户端有类似你似乎试图做的事情。 它是开源的。
从this commit你看,Google称之为“芯片”,你称之为“徽章”。
你会发现关于如何才能实现这样的芯片从上面的承诺,这是我猜的所有信息,是谷歌首次推出这样的芯片(至少到邮件),或在the whole source of the AOSP email client:
将芯片集成到电子邮件中。
更改ID:Ice037a55a169037f725a667fad7714c7e9580b86
我已经把TokenAutoComplete on github我们在Splitwise使用。我在Android SDK中找不到这样的东西,所以我创建了自己的。
我的控制行为与您的预期不符的唯一地方是,当您删除最近完成的令牌时,它会再次变成单词。所有其他令牌都被完全删除。
这里有一个基本的例子:对于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);
}
}
}
布局代码(你可以在这里使用任何种类的布局或可在,如果你想在记号图像抛出的ImageView)
<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>
令牌背景可绘制
<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>
个人目标代码
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上使用。(我还没有试图执行你的评论)。我最小的Android版本是3.0。 – gZerone
对不起,我不能提供更好的答案。我前段时间搜索了一个解决方案,但没有找到任何东西。 Allthough没有什么能够阻止你将现有的代码移植到Android 3.0上,我想你自己想想解决方案会更安全(更快)。之后为什么不开放它? – cimnine
嗨。我认为这超出了我的能力。我正在为我的项目使用另一种方式。我没有再使用Edittext。我觉得它像一个黑客的方式。所以。我不应该公开它。 但是我发现这个链接:http://ballardhack.wordpress.com/2011/07/25/customizing-the-android-edittext-behavior-with-spans/ 我认为这很好,也许可以帮助其他人。 – gZerone