Android搜索框(SearchView)的功能和用法詳解
SearchView是搜索框組件,它可以讓用戶在文本框里輸入文字,通過監(jiān)聽器取得用戶的輸入,當用戶點擊搜索時,監(jiān)聽器執(zhí)行實際的搜索。
1、SearchView組件的常用方法如下:
①setIconifiedByDefault(boolean iconified) ===> 設置搜索框默認是否自動縮小為圖標。
②setOnQueryTextListener(SearchView,OnQueryTextListener listener) ===> 為搜索框設置監(jiān)聽器
③setSubmitButtonEnabled(boolean enabled) ===> 設置是否顯示搜索按鈕
④setQueryHint(CharSequence hint) ===> 設置搜索框內的默認顯示的提示文本
2、為SearchView增加一個配套的ListView,則可以為其增加自動完成的功能,即ListView用于為SearchView顯示自動補齊列表
3、具體實現(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"> <!-- 頂一個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="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; // 自動完成的列表 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);//設置lv可以被過慮 sv = (SearchView) findViewById(R.id.sv); // 設置該SearchView默認是否自動縮小為圖標 sv.setIconifiedByDefault(false); // 為該SearchView組件設置事件監(jiān)聽器 sv.setOnQueryTextListener(this); // 設置該SearchView顯示搜索按鈕 sv.setSubmitButtonEnabled(true); // 設置該SearchView內默認顯示的提示文本 sv.setQueryHint("查找"); } // 用戶輸入字符時激發(fā)該方法 @Override public 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; } // 單擊搜索按鈕時激發(fā)該方法 @Override public boolean onQueryTextSubmit(String query) { // 實際應用中應該在該方法內執(zhí)行實際查詢 // 此處僅使用Toast顯示用戶輸入的查詢內容 Toast.makeText(this, "您的選擇是:" + query, Toast.LENGTH_SHORT).show(); return false; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 解決Android SearchView不顯示搜索icon的問題
- Android開發(fā)之搜索框SearchView用法示例
- Android自定義View實現(xiàn)搜索框(SearchView)功能
- Android仿簡書動態(tài)searchview搜索欄效果
- Android搜索框SearchView屬性和用法詳解
- Android SearchView搜索框組件的使用方法
- Android搜索框組件SearchView的基本使用方法
- 可支持快速搜索篩選的Android自定義選擇控件
- Android自定義控件實現(xiàn)UC瀏覽器語音搜索效果
- Android?SearchView搜索控件使用方法詳解
相關文章
Android使用ContentProvider初始化SDK庫方案小結
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Android 自定義ListView實現(xiàn)QQ空間界面(說說內包含圖片、視頻、點贊、評論、轉發(fā)功能)
這篇文章主要介紹了Android 自定義ListView實現(xiàn)QQ空間界面,qq空間說說內包含圖片、視頻、點贊、評論、轉發(fā)功能,需要的朋友可以參考下2019-12-12