美文网首页
EditText实现搜索框

EditText实现搜索框

作者: XiaoXred | 来源:发表于2020-07-29 11:28 被阅读0次

1.在布局中添加EdidtextView和RecyclerView

<EditText

    android:id="@+id/search_id"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="搜索"

    android:background="@null"

    />

<androidx.recyclerview.widget.RecyclerView

    android:id="@+id/invite_num_detailed"

    android:layout_width="match_parent"

    android:layout_height="wrap_content" />


2.在Activity或者Fragment中找TextBannerView控件

EditText searchId = findViewById(R.id.search_id);

RecyclerView inviteNumDetailed = findViewById(R.id.invite_num_detailed);


3.给RecyclerView设置数据(自己需要搜索的数据列表)

inviteNumDetailed.setLayoutManager(new LinearLayoutManager(this));

SousuoListAdapteradapter adapter =new SousuoListAdapter();

inviteNumDetailed.setAdapter(adapter);


4.给EditText设置监听

searchId.addTextChangedListener(new TextWatcher() {

//text  输入框中改变前的字符串信息

    //start 输入框中改变前的字符串的起始位置

    //count 输入框中改变前后的字符串改变数量一般为0

    //after 输入框中改变后的字符串与起始位置的偏移量

    @Override

    public void beforeTextChanged(CharSequence text, int start, int count,int after) {

}

//text  输入框中改变后的字符串信息

    //start 输入框中改变后的字符串的起始位置

    //before 输入框中改变前的字符串的位置 默认为0

    //count 输入框中改变后的一共输入字符串的数量

    @Override

    public void onTextChanged(CharSequence text, int start, int before, int count) {

if (TextUtils.isEmpty(text)){

adapter.setNewInstance(list);

return;

        }

List data=new ArrayList<>();

        for (int i =0; i

uid =list.get(i).getUid();

            if (String.valueOf(uid).contains(text.toString())){

data.add(list.get(i));

            }

}

adapter.setNewInstance(data);

    }

//edit  输入结束呈现在输入框中的信息

    @Override

    public void afterTextChanged(Editable edit) {

}

});

相关文章

网友评论

      本文标题:EditText实现搜索框

      本文链接:https://www.haomeiwen.com/subject/jnkhrktx.html