android:appComponentFactory,Android MultiAutocompleteTextView

 2023-11-19 阅读 26 评论 0

摘要:In this tutorial, we’ll be discussing and implementing the MultiAutocompleteTextView widget in our application. 在本教程中,我們將在應用程序中討論和實現MultiAutocompleteTextView小部件。 Android MultiAutoCompleteTextView (Android MultiAutoCompleteTe

In this tutorial, we’ll be discussing and implementing the MultiAutocompleteTextView widget in our application.

在本教程中,我們將在應用程序中討論和實現MultiAutocompleteTextView小部件。

Android MultiAutoCompleteTextView (Android MultiAutoCompleteTextView)

MultiAutoCompleteTextView extends AutoCompleteTextView. Unlike AutoCompleteTextView which shows the suggestion for only one string, a MultiAutoCompleteTextView shows you suggestions for each of the substrings you enter separated by a token.

android:appComponentFactory, MultiAutoCompleteTextView擴展了AutoCompleteTextView 。 與僅顯示一個字符串的建議的AutoCompleteTextView不同,MultiAutoCompleteTextView會為您輸入的每個子字符串(由標記分隔)顯示建議。

This feature is fairly common in places such as specifying multiple tags(Ever came across that on StackOverflow or Github?). You must have used that when sending messages to multiple people too.

在指定多個標簽之類的地方,此功能相當普遍(是否曾經在StackOverflow或Github上遇到過?)。 在向多個人發送消息時,您也必須使用過該功能。

An AutoCompleteTextView only offers suggestion about the whole text.
AutoCompleteTextView僅提供有關整個文本的建議。

How is it implemented?

android open automotive protocol, 如何實施?

A Tokenizer instance is set on the MultiAutoCompleteTextView instance. By default in Android, we have a CommaTokenizer built-in class to separate the auto complete strings by commas. Once a string is selected from the dropdown, the comma is appended to mark the end of that substring.

在MultiAutoCompleteTextView實例上設置了Tokenizer實例。 在Android中,默認情況下,我們有一個CommaTokenizer內置類,以逗號分隔自動完成字符串。 一旦從下拉列表中選擇了一個字符串,便會在逗號后添加逗號以標記該子字符串的結尾。

The Tokenizer is set inside the method setTokenizer().

setTokenizer()在方法setTokenizer()

Another important method: setThreshold() is used to specify the number of characters after which the dropdown with the autocomplete suggestions list would be displayed.

另一個重要方法: setThreshold()用于指定字符數,之后將顯示帶有自動完成建議列表的下拉列表。

Let’s jump to the coding part.

讓我們跳到編碼部分。

項目結構 (Project Structure)

(Code)

The code for the activity_main.xml class is given below:

下面給出了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"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="Separated by Commas"android:textSize="18sp" /><MultiAutoCompleteTextViewandroid:id="@+id/multiAutoCompleteTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="20dp"android:ems="10"android:hint="Enter here" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="Separated by Custom Token"android:textSize="18sp" /><MultiAutoCompleteTextViewandroid: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>

The first MultiAutoCompleteTextView could use a Comma Tokenizer. The second would use a custom one – Space Tokenizer.

第一個MultiAutoCompleteTextView可以使用逗號標記器。 第二個將使用自定義的一個-空間令牌生成器。

The code for the SpaceTokenizer.java is given below:

下面給出了SpaceTokenizer.java的代碼:

package com.journaldev.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 + " ";}}}
}

The methods findTokenStart findTokenEnd and terminateToken are a part of the Tokenizer interface and are implemented.

方法findTokenStart findTokenEndterminateToken是標記生成器接口的一部分,并且被實現。

The code for the MainActivity.java class is given below

下面給出MainActivity.java類的代碼

package com.journaldev.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"};@Overrideprotected 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());}
}

The output of the above application in action is given below:

上面應用程序的輸出如下:

This brings an end to this tutorial. You can download the project source code from the link below.

本教程到此結束。 您可以從下面的鏈接下載項目源代碼。

AndroidMultiAutoCompleteTextViewAndroidMultiAutoCompleteTextView
AndroidMultiAutoCompleteTextViewAndroidMultiAutoCompleteTextView

翻譯自: https://www.journaldev.com/22333/android-multiautocompletetextview

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/183170.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息