zoukankan      html  css  js  c++  java
  • Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示

    复制代码
    <?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="wrap_content"
        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:text="清除历史记录" 
                android:onClick="cleanHistory"
                />
        </LinearLayout>
    
    </LinearLayout>
    复制代码
    复制代码
    public class TestActivity extends Activity {
        private AutoCompleteTextView auto;
        private Button searchbtn;
        private ArrayAdapter<String> arr_adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test);
    
            // 初始化
            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);
                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();
            }
    
        }
        
        //清除搜索记录
        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();
        }
        
    }
    复制代码

     实例下载>>>>

    转:http://www.cnblogs.com/tinyphp/p/3973358.html

  • 相关阅读:
    delphi7在windows server 2003企业版上不能打开项目的选项(Options)窗口的解决方法
    简单的两个字“谢谢”,会让我坚持我的写作,我也要谢谢你们
    F41GUT 安装Windows server 2003系统后无法安装显卡驱动的解决办法
    远程桌面无法登录windows server 2003服务器
    F41GUT 安装Windows server 2003系统后无法安装显卡驱动的解决办法
    MS SQL Server 2000版在windows server 2003企业版系统上运行时造成数据库suspect的解决方法
    delphi7在windows server 2003企业版上不能打开项目的选项(Options)窗口的解决方法
    远程桌面无法登录windows server 2003服务器
    MS SQL Server 2000版在windows server 2003企业版系统上运行时造成数据库suspect的解决方法
    关于ajax 和josn
  • 原文地址:https://www.cnblogs.com/Jingerxin/p/5363138.html
Copyright © 2011-2022 走看看