Android中的搜尋方塊(SearchView)的功能和用法

來源:互聯網
上載者:User

標籤:搜尋方塊   searchview功能   searchview用法   listview   

1、SearchView是搜尋方塊組件,它可以讓使用者在文字框裡輸入文字,通過監聽器取得使用者的輸入,當使用者點擊搜尋時,監聽器執行實際的搜尋。


2、SearchView組件的常用方法如下:

①setIconifiedByDefault(boolean iconified) ===> 設定搜尋方塊預設是否自動縮小為表徵圖。
②setOnQueryTextListener(SearchView,OnQueryTextListener listener) ===> 為搜尋方塊設定監聽器
③setSubmitButtonEnabled(boolean enabled) ===> 設定是否顯示搜尋按鈕
④setQueryHint(CharSequence hint) ===> 設定搜尋方塊內的預設顯示的提示文本


3、為SearchView增加一個配套的ListView,則可以為其增加自動完成的功能,即ListView用於為SearchView顯示自動補齊列表


4、具體實現代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- 頂一個SearchView --><SearchViewandroid:id="@+id/sv"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!-- 為SearchView定義自動補齊的ListView--><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/></LinearLayout>

package org.crazyit.ui;import android.os.Bundle;import android.text.TextUtils;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SearchView;import android.widget.Toast;import android.app.Activity;public class SearchViewTest extends Activity implementsSearchView.OnQueryTextListener {private SearchView sv;private ListView lv;// 自動完成的列表private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc", "ddddddd" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);lv = (ListView) findViewById(R.id.lv);lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mStrings));lv.setTextFilterEnabled(true);//設定lv可以被過慮sv = (SearchView) findViewById(R.id.sv);// 設定該SearchView預設是否自動縮小為表徵圖sv.setIconifiedByDefault(false);// 為該SearchView組件設定事件監聽器sv.setOnQueryTextListener(this);// 設定該SearchView顯示搜尋按鈕sv.setSubmitButtonEnabled(true);// 設定該SearchView內預設顯示的提示文本sv.setQueryHint("尋找");}// 使用者輸入字元時激發該方法@Overridepublic boolean onQueryTextChange(String newText) {Toast.makeText(SearchViewTest.this, "textChange--->" + newText, 1).show();if (TextUtils.isEmpty(newText)) {// 清除ListView的過濾lv.clearTextFilter();} else {// 使用使用者輸入的內容對ListView的清單項目進行過濾lv.setFilterText(newText);}return true;}// 單擊搜尋按鈕時激發該方法@Overridepublic boolean onQueryTextSubmit(String query) {// 實際應用中應該在該方法內執行實際查詢// 此處僅使用Toast顯示使用者輸入的查詢內容Toast.makeText(this, "您的選擇是:" + query, Toast.LENGTH_SHORT).show();return false;}}



Android中的搜尋方塊(SearchView)的功能和用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.