Android中AutoCompleteTextView的使用方法

     在我们平常上网的时候经常会用到谷歌或百度,在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,非常方便。这种效果在Android中是用AutoCompleteTextView实现的AutoCompleteTextView是一个可以编辑的文本view,当用户键入时,会自动显示完成建议信息。建议列表显示在下拉列表框中,可以选中某项代替编辑框里的内容。当用户点击回车键时,或者什么也没有选中点击ENTER建时下拉列表会自动消失。建议列表是从一个数据适配器获取的数据。

     AutoCompleteTextView有三个重要的方法clearListSelection():清除选中的列表项、dismissDropDown():如果存在关闭下拉菜单、getAdapter():获取适配器。

     AutoCompleteTextView实际上是一个可输入的TextView,但是,它绑定了一些初始的数据,当用户输入一部分字符(默认是2个字符)后,它会根据绑定的内容而自动匹配,并把符合的结果以下拉菜单的形式显示出来,用户可以单击某个下拉菜单中的选项而完成输入。

     AutoCompleteTextView的一个重要的属性是completionThreshold,这个就是设置用户输入多少个字符后,才匹配,并出现下拉框。

    下面先看看运行的效果。

     首先要建立一个Android的工程,在工程的layout的文件夹下面的main.xml代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        	android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
        	android:text="@string/city" />
    <AutoCompleteTextView 
        	android:id="@+id/auto"
   		android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
   		android:layout_marginLeft="10dp"
   		android:completionThreshold="1"
   		android:hint="@string/inputCityName"/>
  </LinearLayout>


     在main.xml的配置文件中,android:layout_marginLeft=’10dp’’的属性是指距离左边有10dp的距离,android:completionThreshold=’1’的属性表示最少一个字符就开始匹配,其他的属性和别的控件的属性差不多,在这里就不一一讲述了。

     在values文件夹下面的string.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">WeatherTime</string>
    <string name="city">选择城市</string>
    <string name="inputCityName">请输入城市的名称</string>
    <string-array name="citystring">
		<item>北京</item>	
		<item>南京</item>
		<item>南宁</item>
		<item>南昌</item>
    </string-array>
</resources>


      在string.xml文件中, <string-array name="citystring">表示数据源。

     下面给出Java代码:

package com.fx.cx;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteTextViewTest extends Activity {
	private AutoCompleteTextView auto;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        auto = (AutoCompleteTextView)findViewById(R.id.auto);
        ArrayAdapter<?> aAdapter = ArrayAdapter.createFromResource(AutoCompleteTextViewTest.this, R.array.citystring, 
        android.R.layout.simple_spinner_dropdown_item);                                      aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        auto.setAdapter(aAdapter);//设置适配器
    }
}


     首先是通过Id得到AutocompleteTextView的对象,然后把一个已经创建好的ArrayAdapter的适配器放到AutoCompleteTextView的对象里面。ArrayAdapter.createFromResource();这个静态的方法是获得数据源,里面的三个参数分别代表的是传入当前的上下文、string.xml文件中的数据源、以下拉列表的形式呈现出来每一个匹配的Item

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AutocompleteTextView is a subclass of EditText view in Android that shows suggestions automatically while the user is typing. It is commonly used in search functionalities and other similar scenarios. To use AutocompleteTextView in Android Studio, you first need to add the AutoCompleteTextView to your layout XML file. Here's an example: ``` <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter a city name" android:textColorHint="#999" android:completionThreshold="1" android:popupBackground="@color/white" android:dropDownVerticalOffset="10dp" android:elevation="2dp"/> ``` In the above example, `completionThreshold` attribute specifies the minimum number of characters required to trigger the suggestion popup. `popupBackground` attribute specifies the background color of the suggestion popup, and `dropDownVerticalOffset` attribute specifies the vertical offset of the suggestion popup. To provide suggestions to the AutocompleteTextView, you need to set an adapter that provides data to the suggestion popup. Here's an example: ``` AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView); String[] cities = {"New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, cities); autoCompleteTextView.setAdapter(adapter); ``` In the above example, we create an array of cities and provide it to the ArrayAdapter, which then sets the adapter to the AutocompleteTextView. Now, when the user types in the AutocompleteTextView, it will show suggestions based on the provided data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Terry_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值