Android之EditText檢索Listview,匹配關(guān)鍵字高亮


需求背景:在android 開發(fā)中,我們經(jīng)常用EditText通過搜索關(guān)鍵字,來查詢listview里面的內(nèi)容,并且關(guān)鍵字高亮顯示。

基礎(chǔ)知識點

    1. EditText監(jiān)聽文本變化:addTextChangedListener()
  • 2.adapter收到文本變化之后,刷新listview notifyDataSetChanged()

話不多說,直接上代碼:

1.先擼個布局:布局就沒啥好說的啦,簡單粗暴

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="com.yhx.app.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        />


    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


</LinearLayout>

2.當(dāng)然寫個adapter 適配器,這里demo 我就用Listview了,實際項目中,大佬們還是用Recyclerview 哦,這個沒得說。

class ListAdapter extends BaseAdapter {
        private String changeStr = "";
        //
        private List<String> mlistdatas;
        private LayoutInflater mInflater;

        public ListAdapter(Context mcontext, List<String> mlistdatas) {
            this.mlistdatas = mlistdatas;
            mInflater = LayoutInflater.from(mcontext);
        }

        @Override
        public int getCount() {
            return mlistdatas.size();
        }

        @Override
        public Object getItem(int i) {
            return mlistdatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = mInflater.inflate(R.layout.search_layout_item, null);
            TextView tv_name = view.findViewById(R.id.text_name);
            tv_name.setText(mlistdatas.get(i));
              //處理關(guān)鍵字顏色變化
            if (null != mlistdatas.get(i) && mlistdatas.get(i).contains(changeStr)) {
                int index = mlistdatas.get(i).indexOf(changeStr);
                int len = changeStr.length();
                Spanned temp = Html.fromHtml(mlistdatas.get(i).substring(0, index)
                        + "<font color=#ff0000>"
                        + mlistdatas.get(i).substring(index, index + len) + "</font>"
                        + mlistdatas.get(i).substring(index + len, mlistdatas.get(i).length()));
                tv_name.setText(temp);
            } else {
                tv_name.setText(mlistdatas.get(i));
            }

            return view;
        }
        
         //這個方法很重要,editText監(jiān)聽文本變化需要用到
        public void changeText(String textStr) {
            this.changeStr = textStr;
          //別忘了,notifyDataSetChanged()一定要調(diào)用,一定要調(diào)用,一定要調(diào)用,重要事說三遍
            notifyDataSetChanged();

        }
    }

adapter item 布局文件:search_layout_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">


    <TextView
        android:id="@+id/text_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center_vertical"
        android:padding="8dp"
        android:text="簡書你好"
        android:textColor="#333333"/>

</LinearLayout>

Maintivity使用:

public class MainActivity extends AppCompatActivity {
    private ListView mlistview;
    private EditText meditText;
    private ListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlistview = (ListView) findViewById(R.id.listview);
        meditText = (EditText) findViewById(R.id.editText);
        //
        mListAdapter = new ListAdapter(this, initData());
        mlistview.setAdapter(mListAdapter);


        //meditText監(jiān)聽文本變化
        meditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
              //告訴adapter 文本有變化了
                mListAdapter.changeText(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private List<String> initData() {
        List<String> mlistdata = new ArrayList<>();
        mlistdata.add("我的第一寫作,簡書");
        mlistdata.add("很多年以后,開始相信,所謂愛情,都是因為寂寞");
        mlistdata.add("曾經(jīng)企盼有地老天荒的愛情,有海枯石爛的情緣");
        mlistdata.add("愛情不是游戲,因為我們玩不起");
        mlistdata.add("愛情不是游戲,因為我們玩不起");
        mlistdata.add("愛,不是一個人的獨角戲,而是兩個人的對手戲");
        mlistdata.add("愛,不是一個人的獨角戲,而是兩個人的對手戲");
        mlistdata.add("每個人心里,都有個過不去的人");
        mlistdata.add("每個人心里,都有個過不去的人");
        mlistdata.add("異地戀,戀的不僅僅是愛情,還有一種堅持");
        mlistdata.add("異地戀,戀的不僅僅是愛情,還有一種堅持");
        mlistdata.add("曾經(jīng),一遍遍的思念,一遍遍的在心里抱怨");
        mlistdata.add("愛與被愛同樣是受傷害,誰先不愛誰先離開");
        mlistdata.add("還有de小星");
        return mlistdata;

    }
}

少不了老鐵們需要的效果圖,一切源于效果圖說話

yhx.gif

后記:沒有啥技術(shù)含量,但簡單粗暴,實用,不就是這樣嗎 哈哈哈哈。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容