安卓自学笔记:12:用SearchView创建一个搜索框
来源:互联网 发布:图片服务器 windows 编辑:程序博客网 时间:2024/05/17 22:01
主代码:
public class MainActivity extends Activity implementsSearchView.OnQueryTextListener {private SearchView search;private ListView list;private String[] arr = { "aaaaa", "bbbbbb", "ccccccc" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);search = (SearchView) findViewById(R.id.search);list = (ListView) findViewById(R.id.list);list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr));list.setTextFilterEnabled(true);// 设置SearchView是否自动缩小图标search.setIconifiedByDefault(false);// 设置SearchView的监听事件search.setOnQueryTextListener(this);// 设置SearchView显示搜索框search.setSubmitButtonEnabled(true);// 设置SearchView搜索框内的默认显示search.setQueryHint("搜索");}@Overridepublic boolean onQueryTextSubmit(String query) {// TODO Auto-generated method stubToast.makeText(this, "您选择的是" + query, Toast.LENGTH_SHORT).show();return false;}@Overridepublic boolean onQueryTextChange(String newText) {// 用户输入字符时激活该方法if (TextUtils.isEmpty(newText)) {// 清除ListView的过滤list.clearTextFilter();} else {// 是用用户输入的内容对ListView的列表项进行过滤list.setFilterText(newText);}return true;}}
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" > <SearchView android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" > </SearchView><ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="10px" android:layout_weight="1" ></ListView></LinearLayout>
效果图:
0 0
- 安卓自学笔记:12:用SearchView创建一个搜索框
- 安卓自定义搜索框(searchview)
- 安卓自学笔记:5:用ImageView创建一个图片浏览器
- 安卓自学笔记:8:用GridView创建一个带缩略图的图片浏览器
- 安卓自学笔记:9:用AdapterViewFlipper创建一个可以自动播放的图片库
- 安卓自学笔记:6:用ArrayAdapter创建ListView
- 安卓自学笔记:7:用SimpleAdapter创建ListView
- 安卓自学笔记:11:用Toast创建信息提示框
- Android学习笔记(29):搜索框SearchView
- 安卓搜索框SearchView的基本用法(入门级)
- SearchView搜索框
- 搜索框(SearchView)详解
- SearchView搜索框
- SearchView--搜索框案例
- 自定义搜索框searchview
- 安卓自学笔记:4:用Chronometer制作一个简单的计时器
- 一个最简单的基于Android SearchView的搜索框
- 自定义搜索框(SearchView)
- uva11045(最大二分图匹配)
- Ubuntu中文输入法IBUS的安装
- pyenv体验
- 我所钟爱的电影之二-公路片
- HDU 2717 Catch That Cow (BFS)
- 安卓自学笔记:12:用SearchView创建一个搜索框
- C++经典开源项目
- Android中三种解析XML的方法
- 学习笔记——继承中子父类变量,函数,构造函数的特点
- Sizeof与Strlen的区别与联系
- 修改linux swap空间的swappiness,降低对硬盘的缓存
- 重装系统之后的mysql数据恢复,innodb神马的都不重要ibdata idb也都是浮云
- 拦截导弹_JAVA_南阳理工ACM79
- HDU 1873-看病要排队(优先队列+模拟乱搞)