Android搜索框(SearchView)的功能和用法詳解
SearchView是搜索框組件,它可以讓用戶在文本框里輸入文字,通過監(jiān)聽器取得用戶的輸入,當(dāng)用戶點(diǎn)擊搜索時(shí),監(jiān)聽器執(zhí)行實(shí)際的搜索。
1、SearchView組件的常用方法如下:
①setIconifiedByDefault(boolean iconified) ===> 設(shè)置搜索框默認(rèn)是否自動(dòng)縮小為圖標(biāo)。
②setOnQueryTextListener(SearchView,OnQueryTextListener listener) ===> 為搜索框設(shè)置監(jiān)聽器
③setSubmitButtonEnabled(boolean enabled) ===> 設(shè)置是否顯示搜索按鈕
④setQueryHint(CharSequence hint) ===> 設(shè)置搜索框內(nèi)的默認(rèn)顯示的提示文本
2、為SearchView增加一個(gè)配套的ListView,則可以為其增加自動(dòng)完成的功能,即ListView用于為SearchView顯示自動(dòng)補(bǔ)齊列表
3、具體實(shí)現(xiàn)代碼如下:
html
<?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="match_parent" android:orientation="vertical"> <!-- 頂一個(gè)SearchView --> <SearchView android:id="@+id/sv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 為SearchView定義自動(dòng)補(bǔ)齊的ListView--> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
java
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 implements SearchView.OnQueryTextListener { private SearchView sv; private ListView lv; // 自動(dòng)完成的列表 private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc", "ddddddd" }; @Override protected 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);//設(shè)置lv可以被過慮 sv = (SearchView) findViewById(R.id.sv); // 設(shè)置該SearchView默認(rèn)是否自動(dòng)縮小為圖標(biāo) sv.setIconifiedByDefault(false); // 為該SearchView組件設(shè)置事件監(jiān)聽器 sv.setOnQueryTextListener(this); // 設(shè)置該SearchView顯示搜索按鈕 sv.setSubmitButtonEnabled(true); // 設(shè)置該SearchView內(nèi)默認(rèn)顯示的提示文本 sv.setQueryHint("查找"); } // 用戶輸入字符時(shí)激發(fā)該方法 @Override public boolean onQueryTextChange(String newText) { Toast.makeText(SearchViewTest.this, "textChange--->" + newText, 1).show(); if (TextUtils.isEmpty(newText)) { // 清除ListView的過濾 lv.clearTextFilter(); } else { // 使用用戶輸入的內(nèi)容對(duì)ListView的列表項(xiàng)進(jìn)行過濾 lv.setFilterText(newText); } return true; } // 單擊搜索按鈕時(shí)激發(fā)該方法 @Override public boolean onQueryTextSubmit(String query) { // 實(shí)際應(yīng)用中應(yīng)該在該方法內(nèi)執(zhí)行實(shí)際查詢 // 此處僅使用Toast顯示用戶輸入的查詢內(nèi)容 Toast.makeText(this, "您的選擇是:" + query, Toast.LENGTH_SHORT).show(); return false; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Android SearchView不顯示搜索icon的問題
- Android開發(fā)之搜索框SearchView用法示例
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡書動(dòng)態(tài)searchview搜索欄效果
- Android搜索框SearchView屬性和用法詳解
- Android SearchView搜索框組件的使用方法
- Android搜索框組件SearchView的基本使用方法
- 可支持快速搜索篩選的Android自定義選擇控件
- Android自定義控件實(shí)現(xiàn)UC瀏覽器語音搜索效果
- Android?SearchView搜索控件使用方法詳解
相關(guān)文章
Android使用ContentProvider初始化SDK庫方案小結(jié)
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Android自動(dòng)文本框輸入識(shí)別提示功能代碼
這篇文章主要介紹了Android開發(fā)之自動(dòng)文本框輸入識(shí)別提示功能代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android 自定義ListView實(shí)現(xiàn)QQ空間界面(說說內(nèi)包含圖片、視頻、點(diǎn)贊、評(píng)論、轉(zhuǎn)發(fā)功能)
這篇文章主要介紹了Android 自定義ListView實(shí)現(xiàn)QQ空間界面,qq空間說說內(nèi)包含圖片、視頻、點(diǎn)贊、評(píng)論、轉(zhuǎn)發(fā)功能,需要的朋友可以參考下2019-12-12Kotlin中的Checked Exception機(jī)制淺析
這篇文章主要給大家介紹了關(guān)于Kotlin中Checked Exception機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10