android - 如何在 multiautocompletetextview 中使用 gmail 等空间标记器在 android 中正确添加和删除联系人气泡

标签 android autocompletetextview

我一直在努力将气泡添加到 gmail 或 facebook messanger 中的字段。请看下面这张图片.. enter image description here

因此,为了实现上图,我使用这个 sample project 做了一些工作 他们已经给出了实现代码,但我需要用空格分隔每个气泡,这意味着我使用了空格分词器。然后它工作正常但我的问题是如果我继续在 gmail 中的 to 字段中添加联系人到字段正在向上移动并且联系人列表的 ListView 完全显示。但在我的情况下, ListView 在添加最大联系人后没有显示,而且如果我自动添加大长度的联系人姓名,它会为该姓名添加多个气泡。还有一个问题是在 2.2 版移动版中,我无法在接触泡泡之间或之后看到光标。我需要手动点击 contact Bubbles 。我从这个 link 找到了一些消息 但我无法从此 https://android.googlesource.com/platform/frameworks/ex/+/refs/heads/master/chips 导入完整代码.那里有很多依赖项,所有项目都在导入。请让我知道上述问题的任何解决方案。如果有任何 sample 也请张贴在这里..

最佳答案

我开源了我们的解决方案 TokenAutoComplete on github .我的已经测试回 2.2。我设计我的代码以允许非常简单的实现和自定义。我不确定这是否能完全回答您的问题,但它可能是比芯片源代码更好的起点。

这是一个使用我的库的示例实现:

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

contact_token 的布局代码(您可以在此处使用任何类型的布局,或者如果您想要 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>

token 背景可绘制

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

示例 Activity

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);
        completionView.setPrefix("To: ");
    }
}

布局代码

<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>

关于android - 如何在 multiautocompletetextview 中使用 gmail 等空间标记器在 android 中正确添加和删除联系人气泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18561858/

相关文章:

android - Activity 2 中的 BroadcastReceiver 未接收到 Activity 1 中启动的服务发送的广播

android - 如何通过 YouTube 播放器自动播放视频

android - fragment 转换后 AutoCompleteTextView 不起作用

android - 在用它填充 AutoCompleteTextView 之前将 String 数组转换为 ArrayList 有什么好处?

Android: AutoCompleteTextView 隐藏软键盘

android - 如何将监听器添加到自动完成 TextView ,android?

android - 在 test 和 androidTest 之间共享测试类

java - 如何从一次只返回一行的函数中获取 SQLite 的每一行

java - 使用单个适配器显示两个不同类的项目

android - 从 strings.xml 中提取值