日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android實現簡單動態搜索功能_Android

作者:計蒙不吃魚 ? 更新時間: 2022-07-04 編程語言

前言

提到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

欄目分類
最近更新