0

因为某些原因,我需要像这张图片中的红色部分一样的 Edittext:

在此处输入图像描述

每当用户按下键盘上的删除按钮时,edittext 将删除一个标记而不是一个单词。所以,我的问题是:我们是否有类似的存在控件?或者如果没有,你知道如何定制一个。注意:我不需要它 100%。现在,我正在考虑使用 TextWatcher 或 setKeyListener 方法来删​​除功能。

非常感谢您的帮助。对不起,因为我的英语不是很好。

4

2 回答 2

4

在 github 上整理了 TokenAutoComplete供我们在 Splitwise 使用。我在 Android SDK 中找不到类似的东西,所以我自己做了。

我控件的行为与您的期望不符的唯一地方是,当您删除最近完成的令牌时,它会再次变成单词。所有其他令牌都被完全删除。

这是一个基本示例:

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);
        }
    }
}

contact_token 的布局代码(您可以在此处使用任何类型的布局,或者如果您想要令牌中的图像,可以将 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", "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);
    }
}

布局代码

<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>
于 2013-10-03T13:36:55.230 回答
1

The Android AOSP email client has something like you seem trying to do. It is open source.

From this commit you see, that Google calls "chip" what you call "badge".

You should find all information regarding what it takes to implement such chips from the commit above, which I guess, was the first time Google introduced such chips (at least to mail), or in the whole source of the AOSP email client:

Integrate chips into Email.

Change-Id: Ice037a55a169037f725a667fad7714c7e9580b86

于 2012-11-09T16:17:06.570 回答