本篇内容介绍了“Android怎么实现简单动态搜索功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
提到Android的动态搜索,大多应该会想到EditText的文本改变的监听器(addTextChangedListener),本文会简单介绍一下,但是本文介绍的是SearchView+Listview的实现。
效果图:
使用这种方式的思路简述就是,当监听到文本改变时,就用Handler post一个Runnable去做相应的改变,动态修改ListView的显示。
setIconified():设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框
setIconifiedByDefault():设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无X样式点击按钮 有输入内容后有X样式点击按钮 不能关闭搜索框
onActionViewExpanded():设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有X样式点击按钮, 不能关闭搜索框
setOnQueryTextListener():为 SearchView 中的用户操作设置侦听器。
setSubmitButtonEnabled():当查询非空时启用显示提交按钮。
setQueryHint():查询提示语句
本案例使用一个String数组
private final String[] mStrings = Cheeses.sCheeseStrings;
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));
}
//用户输入字符时激发该方法
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>
strings.xml
<string name="cheese_hunt_hint">请输入要查询的内容</string>
Cheeses.java
public class Cheeses {
public static final String[] sCheeseStrings = {
"Android自定义view之3D正方体","计蒙不吃鱼","Android自定义view之利用drawArc方法实现动态效果","Android 3D效果的实现","OkHttp源码解析",
"Android翻转动画(卡片翻转效果)","Android自定义view之围棋动画","Android自定义view之模仿登录界面文本输入框(华为云APP)",
"Android自定义view之太极图","Android自定义view获取attr中自定义颜色的问题","Android对抗反编译","Android常用的room增删改查语句(外部数据库)",
"Android用Canvas画一个折线图,并加以简单封装","Android用Canvas画一个真正能跑的跑马灯","Android网络小说阅读器的实现",
"Android护眼模式(argb)","Android约束布局ConstraintLayout","Android实现EditText的抖动效果"
};
}
“Android怎么实现简单动态搜索功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。