Android学习笔记14——AutoCompleteTextView

前言

在很多App中的搜索栏输入一个或者两个关键词,就会提示用户可能要搜索的结果,这种效果就是使用了AutoCompleteTextView控件,下面分享一下我的学习心得。

AutoCompleteTextView

当用户输入它会自动提供建议,建议列表显示在下拉菜单,从中用户可以选择一个项目,以取代与编辑框的内容。效果如下:
在这里插入图片描述

AutoCompleteTextView的使用

1.准备数据源
2.将数据源数据加载到适配器中
3.将适配器数据加载到控件中

实例Demo

1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:completionHint="请选择您要搜索的内容"/>

</RelativeLayout>

2.设置数据源
在string.xml设置数据源

    <string-array name="country">
        <item>Afghanistan</item>
        <item>Alanian</item>
        <item>Algeria</item>
        <item>American</item>
        <item>Andorra</item>
        <item>Anguilla</item>
    </string-array>

3.Java代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

/**
 * 类说明:演示AutoCompleteTextView 的使用
 */
public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView auto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        auto = findViewById(R.id.auto);
        // 1.准备数据源
        String[] countrys = getResources().getStringArray(R.array.country);
        // 2.将数据源数据加载到适配器中
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,
                countrys);
        // 3.将适配器数据加载到控件中
        auto.setAdapter(adapter);
        // 表示当AutoCompleteTextView控件中某一项被点击的监听事件
        auto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 根据下标获取点击item的信息
                String str = adapterView.getItemAtPosition(i).toString();
                Toast.makeText(MainActivity.this,"当前选中了"+str,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

总结

上面的代码有兴趣的小伙伴可以copy运行一下。上面的内容是我对AutoCompleteTextView的学习,希望对小伙伴们的Android开发有帮助。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值