Gmail style MultiAutoCompleteTextView for Android

2024-03-16 15:40

本文主要是介绍Gmail style MultiAutoCompleteTextView for Android,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

TokenAutoComplete

项目地址:splitwise/TokenAutoComplete 

简介:Gmail style MultiAutoCompleteTextView for Android

Planning note

If you're already using the library, I'm looking at redesigning some of the core components to make this library more reliable and deal with some lingering bugs. Please give me feedback on how you're using it here

Upgrading from 1.* to 2.0

There is one breaking change from 1.* to 2.0. You need to extend TokenCompleteTextView<Object> instead of TokenCompleteTextView.

TokenAutoComplete

TokenAutoComplete is an Android Gmail style token auto-complete text field and filter. It's designed to have an extremely simple API to make it easy for anyone to implement this functionality while still exposing enough customization to let you make it awesome.

Support for Android 4.0.3 (API 15) and up. If you need support for earlier versions of Android, version 1.2.1 is the most recent version that supports Android 2.2 (API 8) and up.

Focused TokenAutoCompleteTextView example

Unfocused TokenAutoCompleteTextView example

Setup

Gradle

dependencies {compile "com.splitwise:tokenautocomplete:2.0.8@aar"
}

Maven

<dependency><groupId>com.splitwise</groupId><artifactId>tokenautocomplete</artifactId><version>2.0.8</version><type>aar</type>
</dependency>

No build tools

Download the jar file and add it to your project

If you would like to get the most recent code in a jar, clone the project and run ./gradlew jar from the root folder. This will build a tokenautocomplete.jar in library/build/libs/.

You may also add the library as an Android Library to your project. All the library files live in library.

Creating your auto complete view

If you'd rather just start with a working example, clone the project and take a look.

For a basic token auto complete view, you'll need to

  1. Subclass TokenCompleteTextView
  2. Create a layout and activity for your completion view

Subclass TokenCompleteTextView

You'll need to provide your own implementations for getViewForObject and defaultObject. You should return a view that displays the token from getViewForObject. In defaultObject, you need to guess what the user meant with their completion. This is usually from the user typing something and hitting "," - see the way gmail for Android handles this for example. Here's a simple example:

public class ContactsCompletionView extends TokenCompleteTextView<Person> {public ContactsCompletionView(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected View getViewForObject(Person person) {LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);TextView view = (TextView) l.inflate(R.layout.contact_token, (ViewGroup) getParent(), false);view.setText(person.getEmail());return view;}@Overrideprotected Person defaultObject(String completionText) {//Stupid simple example of guessing if we have an email or notint index = completionText.indexOf('@');if (index == -1) {return new Person(completionText, completionText.replace(" ", "") + "@example.com");} else {return new Person(completionText.substring(0, index), completionText);}}
}

Layout code for contact_token

<TextView xmlns:android="http://schemas.android.com/apk/res/android"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" />

Token background drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#ffafafaf" /><corners android:radius="5dp" />
</shape>

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; }@Overridepublic String toString() { return name; }
}

Note that the class implements Serializable. In order to restore the view state properly, the TokenCompleteTextView needs to be able to save and restore your objects from disk. If your objects cannot be made Serializable, please look at restoring the view state.

Create a layout and activity for your completion view

I'm adding some very stupid "contacts" to the app so you can see it work, but you should read data from the contacts data provider in a real app.

Activity code

public class TokenActivity extends Activity {ContactsCompletionView completionView;Person[] people;ArrayAdapter<Person> adapter;@Overrideprotected 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);}
}

Layout code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><com.tokenautocomplete.ContactsCompletionViewandroid:id="@+id/searchView"android:layout_width="match_parent"android:layout_height="wrap_content" /></RelativeLayout>

That's it! You can grab the objects the user tokenized with getObjects() on the TokenCompleteTextView when you need to get the data out.

Setting a prefix prompt

If you have a short prompt like "To: ", you can probably get away with setting a drawable on the left side of the TokenCompleteTextView. If you have something longer, you will probably not want your prefix to take up the whole height of the view. If you would like to have a prefix that only indents the first line, you should use setPrefix. This code is a little quirky when restoring the activity, so you want to make sure it only gets called on a fresh start in onCreate:

if (savedInstanceState == null) {completionView.setPrefix("Your bestest friends: ");
}

Custom filtering

If you've used the gmail auto complete, you know that it doesn't use the default "toString" filtering you get with an ArrayAdapter. If you've dug in to the ArrayAdapter, you find an unfortunate mess with no good place for you to add your own custom filter code without re-writing the whole class. If you need to support older versions of Android, this quickly becomes difficult as you'll need to carefully handle API differences for each version.

(NOTE: ArrayAdapter is actually well written, it just doesn't allow for easy custom filters)

I've added my own FilteredArrayAdapter to the jar file that is a subclass of ArrayAdapter but does have some good hooks for custom filtering. You'll want to be fairly efficient in this as it gets called a lot, but it's a simple process to add a custom filter. If you are using the TokenActivity above, you simply replace the line

adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people);

with

adapter = new FilteredArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people) {@Overrideprotected boolean keepObject(Person obj, String mask) {mask = mask.toLowerCase();return obj.getName().toLowerCase().startsWith(mask) || obj.getEmail().toLowerCase().startsWith(mask);}
};

Duplicate objects

In addition to custom filtering, you may want to make sure you don't accidentally miss something and get duplicate tokens. allowDuplicates(false) on the TokenCompleteTextView will prevent any tokens currently in the view from being added a second time. Token objects must implement equals correctly. Any text the user entered for the duplicate token will be cleared.

Responding to user selections

If you're solving a similar problem to Splitwise, you need to handle users adding and removing tokens. I've provided a simple interface to get these events and allow you to respond to them in the TokenCompleteTextView:

public static interface TokenListener {public void onTokenAdded(Object token);public void onTokenRemoved(Object token);
}

We can modify the TokenActivity to see how these callbacks work:

public class TokenActivity extends Activity implements TokenCompleteTextView.TokenListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {/* code from the initial example */completionView.setTokenListener(this);}@Overridepublic void onTokenAdded(Object token) {System.out.println("Added: " + token);}@Overridepublic void onTokenRemoved(Object token) {System.out.println("Removed: " + token);}
}

In Splitwise we use these callbacks to handle users selecting a group when adding an expense. When a user adds a group to an expense, we remove all the users in the group and the other groups from the array adapter. A user should only be able to select one group and it would be redundant to add users in the group to the expense again.

Programatically add and remove objects

You may want to prefill the list with objects. For example when replying to an email, you would want the To: and CC: fields to have the correct emails in them. You can use addObject to put these tokens in. If you are using TokenDeleteStyle.PartialCompletion , you will want to call addObject(obj, "completion text") to get appropriate replacement text, otherwise just call addObject(obj). You can also remove objects programatically with removeObject though this will remove all objects that return true when calling equals on them. If you have copies in the array, you may need to take special care with this. Finally there is a clear function to empty the EditText and remove all the objects.

Letting users click to select and delete tokens

There are three different styles of click handling build into the project. Please open an issue if you need some behavior beyond this with your code! It's relatively easy to add custom click handling, but I'm not convinced anyone would need anything beyond the ones I've provided. Call setTokenClickStyle to change the behavior.

TokenCompleteTextView.TokenClickStyle.None

This is the default, even though it doesn't match the Gmail behavior. When the user clicks on a token, the view will move the cursor in front of or after the token. Users should not be able to get the cursor in the token as this causes confusing behavior.

TokenCompleteTextView.TokenClickStyle.Delete

When the user clicks on a token, the token will be removed from the field. If you need some kind of confirmation, handle it with the onTokenRemoved callback and re-add the token if the user changes their mind.

TokenCompleteTextView.TokenClickStyle.Select

This behavior most closely matches the Gmail token field behavior, but I did not make it the default to simplify the initial tutorial. The first click on a token will unselect any currently selected token views, then it will call setSelected(true) on the selected token. If you want to change the colors of the token, you will need to add appropriate drawables to your project. In the test project, we have the following:

token_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/token_default" android:state_selected="false" /><item android:drawable="@drawable/token_selected" android:state_selected="true" />
</selector>

token_default.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" ><strokeandroid:width="1dp"android:color="#ffd4d4d4" /><solid android:color="#ffafafaf" /><corners android:radius="3dp"/>
</shape>

token_selected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" ><strokeandroid:width="1dp"android:color="#ffa4a4a4" /><solid android:color="#ff7a7a7a" /><corners android:radius="3dp"/>
</shape>

If you need more detailed view customization like changing a picture in the token or resizing the token, you will need to provide a custom view to use in the layout you inflate in getViewForObject and override setSelectedin that view. You can then make appropriate changes to the view.

Example custom view

In a view implementation (see com.tokenautocomplete.TokenTextView):

public class TokenTextView extends TextView {...@Overridepublic void setSelected(boolean selected) {super.setSelected(selected);setCompoundDrawablesWithIntrinsicBounds(0, 0, selected ? R.drawable.close_x : 0, 0);}
}

contact_token.xml

<com.tokenautocomplete.TokenTextViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/token_background"android:textColor="@android:color/white"android:textSize="14sp"android:maxLines="1"android:ellipsize="end"android:padding="4dp"tools:text="Glenda Jönsson" />

Inflate your custom view:

public class ContactsCompletionView extends TokenCompleteTextView<Person> {...@Overrideprotected View getViewForObject(Person person) {LayoutInflater l = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);TokenTextView token = (TokenTextView) l.inflate(R.layout.contact_token, (ViewGroup) getParent(), false);token.setText(person.getEmail());return token;}
}

Custom completion delete behavior

We've defaulted to the gmail style delete handling. That is, the most recently completed token, when deleted, turns into the text that was there before. All other tokens simply disappear when deleted.

While this is the best in our case, we've provided a couple of other options if you want them. Call setDeletionStyle on the TokenCompleteTextView for different behaviors.

TokenCompleteTextView.TokenDeleteStyle.Clear

This is the default. The most recently completed token will turn into the partial completion text it replaces, all other tokens will just disappear when deleted

TokenCompleteTextView.TokenDeleteStyle.PartialCompletion

All tokens will turn into the partial completion text they replaced

TokenCompleteTextView.TokenDeleteStyle.ToString

Tokens will be replaced with the toString value of the objects they represent when they are deleted

Restoring the view state

If your token objects implement Serializable, the TokenCompleteTextView will automatically handle onSaveInstanceState and onRestoreInstanceState. If you cannot make your objects Serializable, you should override getSerializableObjects and convertSerializableArrayToObjectArraygetSerializableObjects should return an array of Serializable objects that can be used to rebuild your original objects when restoring the view state. convertSerializableArrayToObjectArray should take an array of Serializable objects and use them to rebuild your token objects.

We use something similar to this at splitwise to avoid saving complicated object graphs:

@Override
protected ArrayList<Object> convertSerializableArrayToObjectArray(ArrayList<Serializable> sers) {ArrayList<Object> objs = new ArrayList<Object>();for (Serializable s: sers) {if (s instanceof Long) {Contact c = Contact.loadFromDatabase((Long)s);objs.add(c);} else {objs.add(s);}}return objs;
}@Override
protected ArrayList<Serializable> getSerializableObjects() {ArrayList<Serializable> s = new ArrayList<Serializable>();for (Object obj: getObjects()) {if (obj instanceof Serializable) {s.add((Serializable)obj);} else {//obj is a Contacts.add(((Contact)obj).getId());}}return s;
}

Other options

  • Turn off making a best guess when converting text into a token (allows free entry)
    performBestGuess(false);
    
  • Collapse the TokenCompleteTextView to a single line when it loses focus
    allowCollapse(true);
    
  • Change the set of characters that complete a token
    setSplitChar(' ');
    
    OR
    char[] splitChar = {',', ';', ' '};
    setSplitChar(splitChar);
    
  • Change the number of characters required to start showing suggestions
    setThreshold(1);
    
  • Limit the total number of tokens in the field
    setTokenLimit(10);
    
  • Prevent specific tokens from being deleted by overriding isTokenRemovable on your completion view

这篇关于Gmail style MultiAutoCompleteTextView for Android的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/815940

相关文章

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

Android逆向(反调,脱壳,过ssl证书脚本)

文章目录 总结 基础Android基础工具 定位关键代码页面activity定位数据包参数定位堆栈追踪 编写反调脱壳好用的脚本过ssl证书校验抓包反调的脚本打印堆栈bilibili反调的脚本 总结 暑假做了两个月的Android逆向,记录一下自己学到的东西。对于app渗透有了一些思路。 这两个月主要做的是代码分析,对于分析完后的持久化等没有学习。主要是如何反编译源码,如何找到

android系统源码12 修改默认桌面壁纸--SRO方式

1、aosp12修改默认桌面壁纸 代码路径 :frameworks\base\core\res\res\drawable-nodpi 替换成自己的图片即可,不过需要覆盖所有目录下的图片。 由于是静态修改,则需要make一下,重新编译。 2、方法二Overlay方式 由于上述方法有很大缺点,修改多了之后容易遗忘自己修改哪些文件,为此我们采用另外一种方法,使用Overlay方式。