Android:控制項AutoCompleteTextView 用戶端儲存搜尋曆史自動提示

來源:互聯網
上載者:User

標籤:

<?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();    }    }

 執行個體下載>>>>

Android:控制項AutoCompleteTextView 用戶端儲存搜尋曆史自動提示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.