Android在线直播系统源码实现简单的动态搜索
Android在线直播系统源码实现简单的动态搜索相关代码
一、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>
以上就是Android在线直播系统源码实现简单的动态搜索相关代码, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现