Android搜索历史记录与自动填充

二话不说,先来张效果图

界面布局:

<?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:background="@color/bgcolor"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bgcolor"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:background="@null"
            android:drawableLeft="@mipmap/back"
            android:drawablePadding="5dp"
            android:textColor="@color/gray_66"
            android:textSize="14sp" />

        <LinearLayout
            android:id="@+id/keywords"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/search_border_style"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp">


            <AutoCompleteTextView
                android:id="@+id/auto"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:completionHint="最近5条记录"
                android:completionThreshold="1"
                android:ems="10"
                android:hint="@string/search"
                android:textColor="@color/gray_66"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/txt_to_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="搜索"
                android:textColor="@color/gray_66"
                android:textSize="14sp"
                android:visibility="gone" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_search"
            android:layout_width="40dp"
            android:layout_height="35dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:background="@drawable/search_text_style"
            android:gravity="center"
            android:text="搜索"
            android:textColor="@color/bgcolor" />
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/gray" />

</LinearLayout>

代码:

/**
 * 搜索
 * Created by admin on 2017/5/25.
 */

public class Search_Activity extends BaseActivity {
    private AutoCompleteTextView auto;
    private TextView searchbtn;
    private ArrayAdapter<String> arr_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);
        init_create();
    }

    private void init_create() {
        // 初始化
        auto = (AutoCompleteTextView) findViewById(R.id.auto);
        searchbtn = (TextView) findViewById(R.id.tv_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);
            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) {
                // TODO Auto-generated method stub
                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();
        }

    }

}
 就这样完啦,其他的根据需求自定义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值