Android搜索框组件SearchView的基本使用方法

Android搜索框组件SearchView的基本使用方法,第1张

概述SearchView是android系统中内置的一个搜索框组件,可以很方便在添加在用户界面之上,但是也带来了一些问题,那就是searchview的UI是固定的,定制起来会很麻烦,如果对SearchView的要求比较高,完全可以采用button和E

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,null);  }  return cursor; }}

实现的效果如下:

以上就是搜索框组件SearchVIEw的基本使用方法,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android搜索框组件SearchView的基本使用方法全部内容,希望文章能够帮你解决Android搜索框组件SearchView的基本使用方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存