Android 简明教程
Android - Auto Complete
如果您想在可编辑文本字段中输入内容时获得建议,可以通过 AutoCompleteTextView 来实现。当用户输入内容时,它会自动提供建议。建议列表显示在一个下拉菜单中,用户可以选择一个项目以替换编辑框中的内容。
If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
若要使用 AutoCompleteTextView,必须首先在 xml 中创建一个 AutoCompleteTextView 字段。其语法如下所示。
In order to use AutoCompleteTextView you have to first create an AutoCompletTextView Field in the xml. Its syntax is given below.
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" >
之后,您必须在 java 中获取该文本视图的引用。其语法如下所示。
After that, you have to get a reference of this textview in java. Its syntax is given below.
private AutoCompleteTextView actv;
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
接下来,您需要指定要显示的建议项列表。您可以在 java 或 strings.xml 中将列表项指定为字符串数组。其语法如下所示。
The the next thing you need to do is to specify the list of suggestions items to be displayed. You can specify the list items as a string array in java or in strings.xml. Its syntax is given below.
String[] countries = getResources().getStringArray(R.array.list_of_countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);
数组适配器类负责将数据显示为文本字段的建议框中的列表。 setAdapter 方法用于设置 autoCompleteTextView 的适配器。除了这些方法外,自动完成的其他方法如下所示。
The array adapter class is responsible for displaying the data as list in the suggestion box of the text field. The setAdapter method is used to set the adapter of the autoCompleteTextView. Apart from these methods, the other methods of Auto Complete are listed below.
Sr.No |
Method & description |
1 |
getAdapter() This method returns a filterable list adapter used for auto completion |
2 |
getCompletionHint() This method returns optional hint text displayed at the bottom of the the matching list |
3 |
getDropDownAnchor() This method returns returns the id for the view that the auto-complete drop down list is anchored to. |
4 |
getListSelection() This method returns the position of the dropdown view selection, if there is one |
5 |
isPopupShowing() This method indicates whether the popup menu is showing |
6 |
setText(CharSequence text, boolean filter) This method sets text except that it can disable filtering |
7 |
showDropDown() This method displays the drop down on screen. |
Example
以下示例演示了 AutoCompleteTextView 类的使用。它会创建一个基本应用程序,允许您输入文字并显示设备上的建议。
The below example demonstrates the use of AutoCompleteTextView class. It crates a basic application that allows you to type in and it displays suggestions on your device.
要使用此示例,你需要在实际设备或模拟器上运行它。
To experiment with this example , you need to run this on an actual device or in an emulator.
Steps |
Description |
1 |
You will use Android Studio to create an Android application under a package package com.example.sairamkrishna.myapplication. |
2 |
Modify src/MainActivity.java file to add AutoCompleteTextView code |
3 |
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 |
Run the application and choose a running android device and install the application on it and verify the results. |
以下是 src/MainActivity.java 的内容
Here is the content of src/MainActivity.java
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
text1=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
text1.setAdapter(adapter);
text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
以下是 activity_main.xml 的内容。
Here is the content of activity_main.xml
<xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Auto Complete"
android:id="@+id/textView"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorialspoint"
android:id="@+id/textView2"
android:textColor="#ff3eff0f"
android:textSize="35dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_marginTop="72dp"
android:hint="AutoComplete TextView">
<requestFocus />
</AutoCompleteTextView>
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="@+id/autoCompleteTextView1"
android:layout_alignLeft="@+id/autoCompleteTextView1"
android:layout_alignStart="@+id/autoCompleteTextView1"
android:hint="Multi Auto Complete " />
</RelativeLayout>
以下是 Strings.xml 的内容
Here is the content of Strings.xml
<resources>
<string name="app_name">My Application</string>
</resources>
以下是 AndroidManifest.xml 的内容
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我们尝试运行您的应用程序。我假设您在设置环境时已连接了 AVD。若要从 Android Studio 运行该应用程序,请打开您项目的其中一个活动文件,然后从工具栏中单击“运行”图标。Android Studio 将在您的 AVD 中安装此应用程序,并且您的 AVD 将显示以下屏幕。
Let’s try to run your application. I assume you have connected your AVD while doing environment setup. To run the app from Android Studio, open one of your project’s activity files and click Run icon from the toolbar. Android studio will install this application in your AVD and your AVD will display following screen.
现在,只需在文本视图中键入内容即可查看语言建议。由于我只键入了一个字母 as*a*,因此它向我展示了语言建议。
Now just type in the text view to see suggestions of the Languages. As i just type one letter which are as*a*, and it shows me suggestion of language.
multiAutoCompleteTextView 不仅针对单词显示建议,还针对整段文本显示建议。就像在写完第一个单词后,当我开始写第二个单词时,它会向我显示建议。这可以在下面的图片中看到。
The multiAutoCompleteTextView demonstrates suggestions for not only a word but for whole text. As after writing first word , when i start writing the second word , it displays me the suggestions. This can be shown in the picture below.