網站首頁 編程語言 正文
前言
提到Android的動態搜索,大多應該會想到EditText的文本改變的監聽器(addTextChangedListener),本文會簡單介紹一下,但是本文介紹的是SearchView+Listview的實現。
效果圖:
一、addTextChangedListener
使用這種方式的思路簡述就是,當監聽到文本改變時,就用Handler post一個Runnable去做相應的改變,動態修改ListView的顯示。
二、本文案例
1.介紹一下SearchView的一些方法
- setIconified():設置搜索框直接展開顯示。左側有放大鏡(在搜索框中) 右側有叉叉 可以關閉搜索框
- setIconifiedByDefault():設置搜索框直接展開顯示。左側有放大鏡(在搜索框外) 右側無X樣式點擊按鈕 有輸入內容后有X樣式點擊按鈕 不能關閉搜索框
- onActionViewExpanded():設置搜索框直接展開顯示。左側有無放大鏡(在搜索框中) 右側無叉叉 有輸入內容后有X樣式點擊按鈕, 不能關閉搜索框
- setOnQueryTextListener():為 SearchView 中的用戶操作設置偵聽器。
- setSubmitButtonEnabled():當查詢非空時啟用顯示提交按鈕。
- setQueryHint():查詢提示語句
2.準備數據
本案例使用一個String數組
private final String[] mStrings = Cheeses.sCheeseStrings;
3.初始化以及填充數據
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));
? ? ? ? //設置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項,定位到需要的選項。
? ? ? ? mListView.setTextFilterEnabled(true);
? ? ? ? setupSearchView();
private void setupSearchView() {
? ? ? ? //設置搜索框直接展開顯示。左側有放大鏡(在搜索框中) 右側有叉叉 可以關閉搜索框
? ? ? ? //mSearchView.setIconified(false);
? ? ? ? //設置搜索框直接展開顯示。左側有放大鏡(在搜索框外) 右側無叉叉 有輸入內容后有叉叉 不能關閉搜索框
? ? ? ? //mSearchView.setIconifiedByDefault(false);
? ? ? ? //設置搜索框直接展開顯示。左側有無放大鏡(在搜索框中) 右側無叉叉 有輸入內容后有叉叉 不能關閉搜索框
? ? ? ? mSearchView.onActionViewExpanded();
? ? ? ? //為 SearchView 中的用戶操作設置偵聽器。
? ? ? ? mSearchView.setOnQueryTextListener(this);
? ? ? ? //當查詢非空時啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }
4.在SearchView中用戶輸入字符時激發方法里寫入簡單邏輯
//用戶輸入字符時激發該方法
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));
? ? ? ? //設置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項,定位到需要的選項。
? ? ? ? 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() {
? ? ? ? //設置搜索框直接展開顯示。左側有放大鏡(在搜索框中) 右側有叉叉 可以關閉搜索框
? ? ? ? //mSearchView.setIconified(false);
? ? ? ? //設置搜索框直接展開顯示。左側有放大鏡(在搜索框外) 右側無叉叉 有輸入內容后有叉叉 不能關閉搜索框
? ? ? ? //mSearchView.setIconifiedByDefault(false);
? ? ? ? //設置搜索框直接展開顯示。左側有無放大鏡(在搜索框中) 右側無叉叉 有輸入內容后有叉叉 不能關閉搜索框
? ? ? ? mSearchView.onActionViewExpanded();
? ? ? ? //為 SearchView 中的用戶操作設置偵聽器。
? ? ? ? mSearchView.setOnQueryTextListener(this);
? ? ? ? //當查詢非空時啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }
? ? //用戶輸入字符時激發該方法
? ? 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">請輸入要查詢的內容</string>
Cheeses.java
public class Cheeses {
? ? public static final String[] sCheeseStrings = {
? ? ? ? ? ? "Android自定義view之3D正方體","計蒙不吃魚","Android自定義view之利用drawArc方法實現動態效果","Android 3D效果的實現","OkHttp源碼解析",
? ? ? ? ? ? "Android翻轉動畫(卡片翻轉效果)","Android自定義view之圍棋動畫","Android自定義view之模仿登錄界面文本輸入框(華為云APP)",
? ? ? ? ? ? "Android自定義view之太極圖","Android自定義view獲取attr中自定義顏色的問題","Android對抗反編譯","Android常用的room增刪改查語句(外部數據庫)",
? ? ? ? ? ? "Android用Canvas畫一個折線圖,并加以簡單封裝","Android用Canvas畫一個真正能跑的跑馬燈","Android網絡小說閱讀器的實現",
? ? ? ? ? ? "Android護眼模式(argb)","Android約束布局ConstraintLayout","Android實現EditText的抖動效果"
? ? };
}
原文鏈接:https://blog.csdn.net/qq_42761395/article/details/119896875
相關推薦
- 2023-03-20 C#?EF去除重復列DistinctBy方式_C#教程
- 2022-09-25 uniapp封裝request請求的方法
- 2022-04-21 新一代Python包管理工具_python
- 2022-08-12 Android學習之BottomSheetDialog組件的使用_Android
- 2022-04-26 Python?Socket?編程知識點詳細介紹_python
- 2021-12-05 Linux系統運行級別詳細介紹_Linux
- 2022-05-06 SQL查詢服務器下所有數據庫,數據庫的全部表
- 2023-05-14 python隨機獲取列表中某一元素的方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支