AutoCompleteTextView

本文详细介绍了Android中AutoCompleteTextView的使用方法,包括基本配置、属性设置及如何实现本地搜索历史记录的自动提示功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的。

继承关系:

public class AutoCompleteTextView extends 
EditText implements FilterListener {...}

public class EditText extends TextView {...}

基本使用:

自动提示

* XML布局:*

<AutoCompleteTextView
        android:id="@+id/autotext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Java代码:

AutoCompleteTextView autotext = (AutoCompleteTextView) findViewById(R.id.autotext);
        String[] arr = {"aa", "aab", "aac","bb","baa","bac"};
        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
        autotext.setAdapter(arrayAdapter);

AutoCompleteTextView常用属性:

android:completionHint  //设置出现在下拉菜单中的提示标题
android:completionThreshold //设置用户至少输入多少个字符才会显示提示
// 默认是从第二个字符开始匹配的 
//如果设置输入第一个字符就进行提示
//加入下面这行代码autotext.setThreshold(1); 
android:dropDownHorizontalOffset    //下拉菜单于文本框之间的水平偏移。默认与文本框左对齐
android:dropDownHeight  //下拉菜单的高度
android:dropDownWidth   //下拉菜单的宽度
android:singleLine  //单行显示
android:dropDownVerticalOffset  //垂直偏移量

保存本地并自动提示搜索:

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <AutoCompleteTextView
            android:id="@+id/auto"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:completionHint="最近5条记录"
            android:completionThreshold="1"
            />

        <Button
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/clean"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="cleanHistory"
            android:text="清除历史记录"
            />
    </LinearLayout>

</LinearLayout>

保存本地提示

Java代码:

auto = (AutoCompleteTextView) findViewById(R.id.auto);
        searchbtn = (Button) findViewById(R.id.search);

        // 获取搜索记录文件内容
        SharedPreferences sp = getSharedPreferences("search_history", 0);
        String history = sp.getString("history", "暂时没有搜索记录");

        // 用逗号分割内容返回数组
        String[] history_arr = history.split(",");

        // 新建适配器,适配器数据为搜索历史文件内容
        arr_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, history_arr);

        // 保留前50条数据
        if (history_arr.length > 50) {
            String[] newArrays = new String[50];
            // 实现数组之间的复制
            System.arraycopy(history_arr, 0, newArrays, 0, 50);
//System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。
//其函数原型是: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
//src:源数组; srcPos:源数组要复制的起始位置; dest:目的数组; destPos:目的数组放置的起始位置; length:复制的长度。 
//注意:src and dest都必须是同类型或者可以进行转换类型的数组. 有趣的是这个函数可以实现自己到自己复制,
//比如: int[] fun ={0,1,2,3,4,5,6}; System.arraycopy(fun,0,fun,3,3); 
//则结果为:{0,1,2,0,1,2,6}; 实现过程是这样的,先生成一个长度为length的临时数组,
//将fun数组中srcPos 到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时数组,0,fun,3,3).
            arr_adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, history_arr);
        }

        // 设置适配器
        auto.setAdapter(arr_adapter);

        // 设置监听事件,点击搜索写入搜索词
        searchbtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }

        });


  public void save() {
        // 获取搜索框信息
        String text = auto.getText().toString();
        SharedPreferences mysp = getSharedPreferences("search_history", 0);
        String old_text = mysp.getString("history", "暂时没有搜索记录");

        // 利用StringBuilder.append新增内容,逗号便于读取内容时用逗号拆分开
        StringBuilder builder = new StringBuilder(old_text);
        builder.append(text + ",");

        // 判断搜索内容是否已经存在于历史文件,已存在则不重复添加
        if (!old_text.contains(text + ",")) {
            SharedPreferences.Editor myeditor = mysp.edit();
            myeditor.putString("history", builder.toString());
            myeditor.commit();
            Toast.makeText(this, text + "添加成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, text + "已存在", Toast.LENGTH_SHORT).show();
        }

    }

    //清除搜索记录
    public void cleanHistory(View v) {
        SharedPreferences sp = getSharedPreferences("search_history", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }        

Android:控件AutoCompleteTextView 自动提示

Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示

使用System.arraycopy()实现数组之间的复制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值