搜索框(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的事件提供響應。
    運行上面的程序,將看到如下效果:

  

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM