博客
关于我
AutoCompleteTextView输入搜所框
阅读量:188 次
发布时间:2019-02-28

本文共 1377 字,大约阅读时间需要 4 分钟。

在Android开发中,AutoCompleteTextView是一种常用的输入框组件,能够根据用户输入提供相似项的自动完成建议。以下是使用AutoCompleteTextView实现自动完成功能的详细步骤说明。

首先,在布局文件中添加AutoCompleteTextView组件。例如,在res/layout目录下创建一个布局文件auto_complete_layout.xml

接下来,在Activity中获取AutoCompleteTextView组件,并初始化其适配器。例如,在MainActivity.java中:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.auto_complete_layout);        AutoCompleteTextView autoCompleteTextView = findViewById(R.id.edit_layout_auto_compelete);        // 初始化字符串数组        String[] searchList = {"a1", "aa1", "b1", "bb1", "c1", "c2", "123"};        // 创建ArrayAdapter        ArrayAdapter
adapterArr = new ArrayAdapter<>(this, R.layout.auto_complete_layout, searchList); // 设置适配器 autoCompleteTextView.setAdapter(adapterArr); // 设置提示阈值为1,即输入1个字符后显示建议 autoCompleteTextView.setThreshold(1); // 设置输入类型为自动完成输入 autoCompleteTextView.setInputType(InputType.TYPE_TEXT_VARIATION_AUTOCOMPLETE); }}

这样,AutoCompleteTextView将根据用户输入提示匹配的内容。例如,用户输入"c"时,会显示"c1"和"c2"等匹配项。

需要注意的是,在Android 11及以上版本中,需要使用androidx.cursor包裹的Cursor管理器来支持AutoCompleteTextView功能。如上所示,添加了app:layoutManager="androidx.cursor.Cursor管理器"以支持最新版本。

通过以上步骤,可以轻松实现AutoCompleteTextView的自动完成功能,提升用户输入体验。

转载地址:http://itqn.baihongyu.com/

你可能感兴趣的文章
poj 1236(强连通分量分解模板题)
查看>>
poj 1258 Agri-Net
查看>>
quagga 和 zebos
查看>>
poj 1286 Necklace of Beads
查看>>
POJ 1321 棋盘问题
查看>>
poj 1321(回溯)
查看>>
Qt高级——Qt元对象系统源码解析
查看>>
qt调用vs2008编写的dll动态库(隐式调用)
查看>>
Qt读取注册表默认值
查看>>
poj 1679 判断MST是不是唯一的 (次小生成树)
查看>>
POJ 1703 Find them, Catch them
查看>>
POJ 1703 Find them, Catch them 并查集
查看>>
POJ 1738 An old Stone Game(石子合并)
查看>>
POJ 1740 A New Stone Game(博弈)题解
查看>>
Qt网络编程之实例二POST方式
查看>>
POJ 1765 November Rain
查看>>
poj 1860 Currency Exchange
查看>>
POJ 1961 Period
查看>>
POJ 2019 Cornfields (二维RMQ)
查看>>
poj 2057 The Lost House 贪心思想在动态规划上的应用
查看>>