Android實現(xiàn)簡單動態(tài)搜索功能
前言
提到Android的動態(tài)搜索,大多應(yīng)該會想到EditText的文本改變的監(jiān)聽器(addTextChangedListener),本文會簡單介紹一下,但是本文介紹的是SearchView+Listview的實現(xiàn)。
效果圖:

一、addTextChangedListener
使用這種方式的思路簡述就是,當(dāng)監(jiān)聽到文本改變時,就用Handler post一個Runnable去做相應(yīng)的改變,動態(tài)修改ListView的顯示。
二、本文案例
1.介紹一下SearchView的一些方法
- setIconified():設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框中) 右側(cè)有叉叉 可以關(guān)閉搜索框
- setIconifiedByDefault():設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框外) 右側(cè)無X樣式點擊按鈕 有輸入內(nèi)容后有X樣式點擊按鈕 不能關(guān)閉搜索框
- onActionViewExpanded():設(shè)置搜索框直接展開顯示。左側(cè)有無放大鏡(在搜索框中) 右側(cè)無叉叉 有輸入內(nèi)容后有X樣式點擊按鈕, 不能關(guān)閉搜索框
- setOnQueryTextListener():為 SearchView 中的用戶操作設(shè)置偵聽器。
- setSubmitButtonEnabled():當(dāng)查詢非空時啟用顯示提交按鈕。
- setQueryHint():查詢提示語句
2.準(zhǔn)備數(shù)據(jù)
本案例使用一個String數(shù)組
private final String[] mStrings = Cheeses.sCheeseStrings;
3.初始化以及填充數(shù)據(jù)
mSearchView = (SearchView) findViewById(R.id.search_view); ? ? ? ? mListView = (ListView) findViewById(R.id.list_view); ? ? ? ? mListView.setAdapter(mAdapter = new ArrayAdapter<>(this, ? ? ? ? ? ? ? ? android.R.layout.simple_list_item_1, ? ? ? ? ? ? ? ? mStrings)); ? ? ? ? //設(shè)置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項,定位到需要的選項。 ? ? ? ? mListView.setTextFilterEnabled(true); ? ? ? ? setupSearchView();
private void setupSearchView() {
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框中) 右側(cè)有叉叉 可以關(guān)閉搜索框
? ? ? ? //mSearchView.setIconified(false);
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框外) 右側(cè)無叉叉 有輸入內(nèi)容后有叉叉 不能關(guān)閉搜索框
? ? ? ? //mSearchView.setIconifiedByDefault(false);
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有無放大鏡(在搜索框中) 右側(cè)無叉叉 有輸入內(nèi)容后有叉叉 不能關(guān)閉搜索框
? ? ? ? mSearchView.onActionViewExpanded();
? ? ? ? //為 SearchView 中的用戶操作設(shè)置偵聽器。
? ? ? ? mSearchView.setOnQueryTextListener(this);
? ? ? ? //當(dāng)查詢非空時啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }4.在SearchView中用戶輸入字符時激發(fā)方法里寫入簡單邏輯
//用戶輸入字符時激發(fā)該方法
public boolean onQueryTextChange(String newText) {
? ? ? ? if (TextUtils.isEmpty(newText)) {
? ? ? ? ? ? mListView.clearTextFilter();
? ? ? ? } else {
? ? ? ? ? ? mListView.setFilterText(newText.toString());
? ? ? ? }
? ? ? ? return true;
? ? }三、源碼
JimengSearchView.java
public class JimengSearchView extends Activity implements SearchView.OnQueryTextListener {
? ? private SearchView mSearchView;
? ? private ListView mListView;
? ? private ArrayAdapter<String> mAdapter;
? ? private final String[] mStrings = Cheeses.sCheeseStrings;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
? ? ? ? setContentView(R.layout.searchview_filter);
? ? ? ? mSearchView = (SearchView) findViewById(R.id.search_view);
? ? ? ? mListView = (ListView) findViewById(R.id.list_view);
? ? ? ? mListView.setAdapter(mAdapter = new ArrayAdapter<>(this,
? ? ? ? ? ? ? ? android.R.layout.simple_list_item_1,
? ? ? ? ? ? ? ? mStrings));
? ? ? ? //設(shè)置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項,定位到需要的選項。
? ? ? ? mListView.setTextFilterEnabled(true);
? ? ? ? setupSearchView();
? ? ? ? mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
? ? ? ? ? ? ? ? String str = (String)((TextView) view).getText();
? ? ? ? ? ? ? ? Toast.makeText(JimengSearchView.this,str,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void setupSearchView() {
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框中) 右側(cè)有叉叉 可以關(guān)閉搜索框
? ? ? ? //mSearchView.setIconified(false);
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框外) 右側(cè)無叉叉 有輸入內(nèi)容后有叉叉 不能關(guān)閉搜索框
? ? ? ? //mSearchView.setIconifiedByDefault(false);
? ? ? ? //設(shè)置搜索框直接展開顯示。左側(cè)有無放大鏡(在搜索框中) 右側(cè)無叉叉 有輸入內(nèi)容后有叉叉 不能關(guān)閉搜索框
? ? ? ? mSearchView.onActionViewExpanded();
? ? ? ? //為 SearchView 中的用戶操作設(shè)置偵聽器。
? ? ? ? mSearchView.setOnQueryTextListener(this);
? ? ? ? //當(dāng)查詢非空時啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }
? ? //用戶輸入字符時激發(fā)該方法
? ? public boolean onQueryTextChange(String newText) {
? ? ? ? if (TextUtils.isEmpty(newText)) {
? ? ? ? ? ? mListView.clearTextFilter();
? ? ? ? } else {
? ? ? ? ? ? mListView.setFilterText(newText.toString());
? ? ? ? }
? ? ? ? return true;
? ? }
? ? public boolean onQueryTextSubmit(String query) {
? ? ? ? return false;
? ? }
}布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="vertical"> ? ? <SearchView ? ? ? ? ? ? android:id="@+id/search_view" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content"/> ? ? <ListView ? ? ? ? ? ? android:id="@+id/list_view" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1"/> </LinearLayout>
strings.xml
<string name="cheese_hunt_hint">請輸入要查詢的內(nèi)容</string>
Cheeses.java
public class Cheeses {
? ? public static final String[] sCheeseStrings = {
? ? ? ? ? ? "Android自定義view之3D正方體","計蒙不吃魚","Android自定義view之利用drawArc方法實現(xiàn)動態(tài)效果","Android 3D效果的實現(xiàn)","OkHttp源碼解析",
? ? ? ? ? ? "Android翻轉(zhuǎn)動畫(卡片翻轉(zhuǎn)效果)","Android自定義view之圍棋動畫","Android自定義view之模仿登錄界面文本輸入框(華為云APP)",
? ? ? ? ? ? "Android自定義view之太極圖","Android自定義view獲取attr中自定義顏色的問題","Android對抗反編譯","Android常用的room增刪改查語句(外部數(shù)據(jù)庫)",
? ? ? ? ? ? "Android用Canvas畫一個折線圖,并加以簡單封裝","Android用Canvas畫一個真正能跑的跑馬燈","Android網(wǎng)絡(luò)小說閱讀器的實現(xiàn)",
? ? ? ? ? ? "Android護眼模式(argb)","Android約束布局ConstraintLayout","Android實現(xiàn)EditText的抖動效果"
? ? };
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程設(shè)計模式之狀態(tài)模式詳解
這篇文章主要介紹了Android編程設(shè)計模式之狀態(tài)模式,結(jié)合實例形式詳細(xì)分析了Android狀態(tài)模式的概念、功能、使用方法及相關(guān)注意事項,需要的朋友可以參考下2017-12-12
Android 微信搖一搖功能實現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Android 微信搖一搖功能實現(xiàn)詳細(xì)介紹的相關(guān)資料,并附實例代碼及實現(xiàn)微信搖一搖的思路,需要的朋友可以參考下2016-11-11
Android Retrofit2數(shù)據(jù)解析代碼解析
這篇文章主要介紹了Android Retrofit2數(shù)據(jù)解析代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

