Android MultiAutocompleteTextView
时间:2020-02-23 14:29:04 来源:igfitidea点击:
在本教程中,我们将在应用程序中讨论和实现MultiAutocompleteTextView小部件。
Android MultiAutoCompleteTextView
MultiAutoCompleteTextView扩展了AutoCompleteTextView。
与仅显示一个字符串的建议的AutoCompleteTextView不同,MultiAutoCompleteTextView会为您输入的每个子字符串(由标记分隔)显示建议。
在指定多个标签(在StackOverflow或者Github上遇到过这种标签)之类的地方,此功能相当普遍。
在向多个人发送消息时,您也必须使用过该功能。
AutoCompleteTextView仅提供有关整个文本的建议。
如何实施?
在MultiAutoCompleteTextView实例上设置了Tokenizer实例。
在Android中,默认情况下,我们有一个CommaTokenizer内置类,以逗号分隔自动完成字符串。
从下拉列表中选择一个字符串后,将在逗号后附加该子字符串的结尾。
令牌生成器在方法setTokenizer()中设置。
另一个重要方法:setThreshold()用于指定字符数,之后将显示带有自动完成建议列表的下拉列表。
代码
下面给出了activity_main.xml类的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Separated by Commas"
android:textSize="18sp"
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter here"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Separated by Custom Token"
android:textSize="18sp"
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextViewCustom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Add tags here"
</LinearLayout>
第一个MultiAutoCompleteTextView可以使用逗号标记器。
第二个将使用自定义的-空间标记器。
下面给出了SpaceTokenizer.java的代码:
package com.theitroad.androidmultiautocompletetextview;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;
public class SpaceTokenizer implements MultiAutoCompleteTextView.Tokenizer {
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == ' ') {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
}
方法findTokenStart,findTokenEnd和terminateToken是Tokenizer接口的一部分并已实现。
下面给出了MainActivity.java类的代码
package com.theitroad.androidmultiautocompletetextview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
MultiAutoCompleteTextView multiAutoCompleteTextView, multiAutoCompleteTextViewCustom;
String[] randomSuggestions = {"a", "aa", "ab", "aab", "abc", "abcd", "abcde", "abcdef"};
String[] tags = {"Java", "JavaScript", "Spring", "Java EE", "Java 8", "Java 9", "Java 10", "SQL", "SQLite"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView);
multiAutoCompleteTextViewCustom = findViewById(R.id.multiAutoCompleteTextViewCustom);
ArrayAdapter<String> randomArray = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, randomSuggestions);
multiAutoCompleteTextView.setAdapter(randomArray);
multiAutoCompleteTextView.setThreshold(1);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
ArrayAdapter<String> tagArray = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, tags);
multiAutoCompleteTextViewCustom.setAdapter(tagArray);
multiAutoCompleteTextViewCustom.setThreshold(2);
multiAutoCompleteTextViewCustom.setTokenizer(new SpaceTokenizer());
}
}

