Android搜索框组件SearchView的基本使用方法
程序员文章站
2024-03-01 16:26:04
searchview是android系统中内置的一个搜索框组件,可以很方便在添加在用户界面之上,但是也带来了一些问题,那就是searchview的ui是固定的,定制起来会很...
searchview是android系统中内置的一个搜索框组件,可以很方便在添加在用户界面之上,但是也带来了一些问题,那就是searchview的ui是固定的,定制起来会很麻烦,如果对searchview的要求比较高,完全可以采用button和edittext自己实现。这里先简单的说说searchview的使用:
main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".main" > <searchview android:id="@+id/sv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeoptions="actiongo" /> </linearlayout>
在显示suggestion的时候会用到下面的布局文件:mytextview.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="50sp" android:orientation="vertical" > <textview android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingleft="5sp" android:textsize="18sp" /> </linearlayout>
main.java:
package com.app.main; import java.lang.reflect.field; import android.app.activity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.imageview; import android.widget.listview; import android.widget.searchview; import android.widget.searchview.onquerytextlistener; import android.widget.simplecursoradapter; import android.widget.toast; public class main extends activity { searchview sv = null; listview lv = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); sv = (searchview) this.findviewbyid(r.id.sv); sv.seticonifiedbydefault(false); sv.setsubmitbuttonenabled(true); sv.setqueryhint("查询"); //通过反射,修改默认的样式,可以从android的search_view.xml中找到需要的组件 try { field field = sv.getclass().getdeclaredfield("msubmitbutton"); field.setaccessible(true); imageview iv = (imageview) field.get(sv); iv.setimagedrawable(this.getresources().getdrawable( r.drawable.pointer)); } catch (exception e) { e.printstacktrace(); } cursor cursor = this.gettestcursor(); @suppresswarnings("deprecation") simplecursoradapter adapter = new simplecursoradapter(this, r.layout.mytextview, cursor, new string[] { "tb_name" }, new int[] { r.id.textview }); sv.setsuggestionsadapter(adapter); sv.setonquerytextlistener(new onquerytextlistener() { @override public boolean onquerytextchange(string str) { return false; } @override public boolean onquerytextsubmit(string str) { toast.maketext(main.this, str, toast.length_short).show(); return false; } }); } //添加suggestion需要的数据 public cursor gettestcursor() { sqlitedatabase db = sqlitedatabase.openorcreatedatabase( this.getfilesdir() + "/my.db3", null); cursor cursor = null; try { string insertsql = "insert into tb_test values (null,?,?)"; db.execsql(insertsql, new object[] { "aa", 1 }); db.execsql(insertsql, new object[] { "ab", 2 }); db.execsql(insertsql, new object[] { "ac", 3 }); db.execsql(insertsql, new object[] { "ad", 4 }); db.execsql(insertsql, new object[] { "ae", 5 }); string querysql = "select * from tb_test"; cursor = db.rawquery(querysql, null); } catch (exception e) { string sql = "create table tb_test (_id integer primary key autoincrement,tb_name varchar(20),tb_age integer)"; db.execsql(sql); string insertsql = "insert into tb_test values (null,?,?)"; db.execsql(insertsql, new object[] { "aa", 1 }); db.execsql(insertsql, new object[] { "ab", 2 }); db.execsql(insertsql, new object[] { "ac", 3 }); db.execsql(insertsql, new object[] { "ad", 4 }); db.execsql(insertsql, new object[] { "ae", 5 }); string querysql = "select * from tb_test"; cursor = db.rawquery(querysql, null); } return cursor; } }
实现的效果如下:
以上就是搜索框组件searchview的基本使用方法,希望能给大家一个参考,也希望大家多多支持。