天天看點

搜尋框(SearchView)的功能與用法

    SearchView是搜尋框元件,它可以讓使用者在文本框内輸入漢字,并允許通過監聽器監控使用者輸入,當使用者使用者輸入完成後送出搜尋按鈕時,也通過監聽器執行實際的搜尋。

    使用SearchView時可以使用如下常用方法。

  • setIconifiedByDefault(boolean iconified):設定該搜尋框預設是否自動縮小為圖示。
  • setSubmitButtonEnabled(boolean enabled):設定是否顯示搜尋按鈕。
  • setQueryHint(CharSequence hint):設定搜尋框内預設顯示的提示文本。
  • setOnQueryTextListener(SearchView.OnQueryTextListener listener):為該搜尋框設定事件監聽器。

    如果為SearchView增加一個配套的ListView,則可以為SearchView增加自動完成的功能。如下執行個體示範了SearchView的功能與用法。

    執行個體:搜尋   

    該執行個體的界面布局檔案中定義了一個SearchView和ListView,其中ListView用于為SearchView顯示自動補齊清單。界面布局檔案如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <!-- 定義一個SearchView -->
   <SearchView android:id="@+id/sv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
   <!-- 為SearchView定義自動完成的ListView -->
   <ListView android:id="@+id/lv"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
      />
</LinearLayout>      

      上面的布局檔案中定義了一個SearchView元件,并為該SearchView元件定義了一個ListView元件,該ListView元件用于為SearchView元件顯示自動完成清單。

      下面是該執行個體對應的Activity代碼。

      該Activity對應的背景代碼檔案如下:

package org.crazyit.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.*;

public class SearchViewTest extends Activity implements SearchView.OnQueryTextListener {

    
    private SearchView sv;
    private ListView lv;
    //自動完成的清單
    private final String[] mStrings={"aaaaaa","bbbbbb","cccccc"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_view_test);
        lv=(ListView)findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));
        lv.setTextFilterEnabled(true);
        
        sv=(SearchView)findViewById(R.id.sv);
        //設定該SearchView預設是否自動縮小為圖示
        sv.setIconifiedByDefault(false);
        //為該SearchView元件設定事件監聽器
        sv.setOnQueryTextListener(this);
        //設定該SearchView顯示搜尋按鈕
        sv.setSubmitButtonEnabled(true);

        //設定該SearchView内預設顯示的提示文本
        sv.setQueryHint("查找");
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_view_test, menu);
        return true;
    }
    //使用者輸入字元時激發該方法
    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        if(TextUtils.isEmpty(newText))
        {
            //清楚ListView的過濾
            lv.clearTextFilter();
        }
        else
        {
            //使用使用者輸入的内容對ListView的清單項進行過濾
            lv.setFilterText(newText);
        
        }
        return true;
    }
     //單擊搜尋按鈕時激發該方法
    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        //實際應用中應該在該方法内執行實際查詢
        //此處僅使用Toast顯示使用者輸入的查詢内容
    Toast.makeText(this, "您選擇的是:"+query, Toast.LENGTH_SHORT).show();
        return true;
    }

}      

上面的程式中粗體字代碼就是控制SearchView的關鍵代碼,第一段粗體字代碼我iSearchView設定了事件監聽器,并為該SearchView啟用了搜尋按鈕。接下來程式重寫了onQueryTextChange()、onQueryTextSubmit()兩個方法,這兩個方法用于為SearchView的事件提供響應。

    運作上面的程式,将看到如下效果:

搜尋框(SearchView)的功能與用法
搜尋框(SearchView)的功能與用法