亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能

 更新時(shí)間:2022年05月11日 08:29:03   作者:計(jì)蒙不吃魚  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

提到Android的動(dòng)態(tài)搜索,大多應(yīng)該會(huì)想到EditText的文本改變的監(jiān)聽器(addTextChangedListener),本文會(huì)簡單介紹一下,但是本文介紹的是SearchView+Listview的實(shí)現(xiàn)。

效果圖:

一、addTextChangedListener

使用這種方式的思路簡述就是,當(dāng)監(jiān)聽到文本改變時(shí),就用Handler post一個(gè)Runnable去做相應(yīng)的改變,動(dòng)態(tài)修改ListView的顯示。

二、本文案例

1.介紹一下SearchView的一些方法

  • setIconified():設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框中) 右側(cè)有叉叉 可以關(guān)閉搜索框
  • setIconifiedByDefault():設(shè)置搜索框直接展開顯示。左側(cè)有放大鏡(在搜索框外) 右側(cè)無X樣式點(diǎn)擊按鈕 有輸入內(nèi)容后有X樣式點(diǎn)擊按鈕 不能關(guān)閉搜索框
  • onActionViewExpanded():設(shè)置搜索框直接展開顯示。左側(cè)有無放大鏡(在搜索框中) 右側(cè)無叉叉 有輸入內(nèi)容后有X樣式點(diǎn)擊按鈕, 不能關(guān)閉搜索框
  • setOnQueryTextListener():為 SearchView 中的用戶操作設(shè)置偵聽器。
  • setSubmitButtonEnabled():當(dāng)查詢非空時(shí)啟用顯示提交按鈕。
  • setQueryHint():查詢提示語句

2.準(zhǔn)備數(shù)據(jù)

本案例使用一個(gè)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è)置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項(xiàng),定位到需要的選項(xiàng)。
? ? ? ? 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)查詢非空時(shí)啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }

4.在SearchView中用戶輸入字符時(shí)激發(fā)方法里寫入簡單邏輯

//用戶輸入字符時(shí)激發(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è)置是否可以通過鍵盤輸入的字符來過濾掉不需要的選項(xiàng),定位到需要的選項(xiàng)。
? ? ? ? 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)查詢非空時(shí)啟用顯示提交按鈕。
? ? ? ? mSearchView.setSubmitButtonEnabled(false);
? ? ? ? //查詢提示語句
? ? ? ? mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
? ? }
? ? //用戶輸入字符時(shí)激發(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">請(qǐng)輸入要查詢的內(nèi)容</string>

Cheeses.java

public class Cheeses {

? ? public static final String[] sCheeseStrings = {
? ? ? ? ? ? "Android自定義view之3D正方體","計(jì)蒙不吃魚","Android自定義view之利用drawArc方法實(shí)現(xiàn)動(dòng)態(tài)效果","Android 3D效果的實(shí)現(xiàn)","OkHttp源碼解析",
? ? ? ? ? ? "Android翻轉(zhuǎn)動(dòng)畫(卡片翻轉(zhuǎn)效果)","Android自定義view之圍棋動(dòng)畫","Android自定義view之模仿登錄界面文本輸入框(華為云APP)",
? ? ? ? ? ? "Android自定義view之太極圖","Android自定義view獲取attr中自定義顏色的問題","Android對(duì)抗反編譯","Android常用的room增刪改查語句(外部數(shù)據(jù)庫)",
? ? ? ? ? ? "Android用Canvas畫一個(gè)折線圖,并加以簡單封裝","Android用Canvas畫一個(gè)真正能跑的跑馬燈","Android網(wǎng)絡(luò)小說閱讀器的實(shí)現(xiàn)",
? ? ? ? ? ? "Android護(hù)眼模式(argb)","Android約束布局ConstraintLayout","Android實(shí)現(xiàn)EditText的抖動(dòng)效果"
? ? };

}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論