AutocompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing in android apps. In this tutorial we’ll implement android AutoCompleteTextView in our application using an ArrayAdapter
to define the list of suggestions.
AutocompleteTextView是可编辑的文本视图,当用户在android应用中键入内容时,它会自动显示完成建议。 在本教程中,我们将使用ArrayAdapter
在我们的应用程序中实现android AutoCompleteTextView来定义建议列表。
AutoCompleteTextView is a component used to show suggestions while writing in an editable text field. The suggestions list is shown in a drop down menu from which a user can select the desired item. The list of suggestions is obtained from an adapter and it appears only after a number of characters that are specified in the threshold.
AutoCompleteTextView是一个组件,用于在可编辑文本字段中书写时显示建议。 建议列表显示在下拉菜单中,用户可以从中选择所需的项目。 建议列表是从适配器获得的,仅在阈值中指定的多个字符之后出现。
In order to use an AutoCompleteThreshold field, it needs to be defined in the xml layout as follows:
为了使用AutoCompleteThreshold字段,需要在xml布局中定义它,如下所示:
Note : android:ems or setEms(x) sets the width of a TextView to fit a text of x ‘M’ number of letters regardless of the actual text extension and text size.
注意: android:ems或setEms(x)设置TextView的宽度以适合x'M '个字母的文本,而不管实际的文本扩展名和文本大小如何。
Some important methods of Autocomplete list are given below:
自动完成列表的一些重要方法如下:
The setAdapter method is used to set the adapter of the autoCompleteTextView.
setAdapter方法用于设置autoCompleteTextView的适配器。
Let’s jump to the coding part of it.
让我们跳到它的编码部分。
This project contains a simple TextView
and a AutoCompleteTextView
in the layout of the MainActivity. The ArrayAdapter contains the following fruits : Apple, Banana, Cherry, Date, Grape, Kiwi, Mango, Pear.
该项目在MainActivity的布局中包含一个简单的TextView
和一个AutoCompleteTextView
。 ArrayAdapter包含以下水果:苹果,香蕉,樱桃,枣,葡萄,猕猴桃,芒果,梨。
The layout of the MainActivity is defined as follows.
MainActivity的布局定义如下。
activity_main.xml
activity_main.xml
The MainActivity.java is defined below.
MainActivity.java在下面定义。
package com.journaldev.autocomplete;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter adapter = new ArrayAdapter
(this, android.R.layout.select_dialog_item, fruits);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
}
}
In the above code, we’ve stored the list of fruits in an ArrayAdapter with a layout imported from the Android SDK.
在上面的代码中,我们将水果列表存储在具有从Android SDK导入的布局的ArrayAdapter中。
The text color in the editable TextView is red. A threshold count of 1 depicts that to show the autocomplete drop-down list we need to type in at least one character.
可编辑TextView中的文本颜色为红色。 阈值计数为1表示要显示自动完成下拉列表,我们需要至少键入一个字符。
Note: Autocomplete list is visible only when the Editable field is focused.
注意 :仅当“可编辑”字段为焦点时,“自动完成列表”才可见。
Below is our auto complete example application in execution.
以下是我们正在执行的自动完成示例应用程序。
This brings an end to Android AutoCompleteTextView tutorial. You can download the final Android AutoComplete project from the link given below.
这样就结束了Android AutoCompleteTextView教程。 您可以从下面的链接下载最终的Android AutoComplete项目 。
翻译自: https://www.journaldev.com/9574/android-autocompletetextview-example-tutorial