溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-05-12 14:41:35 來源:億速云 閱讀:410 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Android怎么實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)搜索功能”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

    前言

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

    效果圖:

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

    一、addTextChangedListener

    使用這種方式的思路簡(jiǎn)述就是,當(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():查詢提示語(yǔ)句

    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);
            //查詢提示語(yǔ)句
            mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
        }

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

    //用戶輸入字符時(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);
            //查詢提示語(yǔ)句
            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增刪改查語(yǔ)句(外部數(shù)據(jù)庫(kù))",
                "Android用Canvas畫一個(gè)折線圖,并加以簡(jiǎn)單封裝","Android用Canvas畫一個(gè)真正能跑的跑馬燈","Android網(wǎng)絡(luò)小說閱讀器的實(shí)現(xiàn)",
                "Android護(hù)眼模式(argb)","Android約束布局ConstraintLayout","Android實(shí)現(xiàn)EditText的抖動(dòng)效果"
        };
    
    }

    “Android怎么實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)搜索功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI