国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-12-12 06:20:19
字體:
供稿:網(wǎng)友

SearchView是android系統(tǒng)中內(nèi)置的一個(gè)搜索框組件,可以很方便在添加在用戶界面之上,但是也帶來了一些問題,那就是searchview的UI是固定的,定制起來會(huì)很麻煩,如果對SearchView的要求比較高,完全可以采用button和EditText自己實(shí)現(xiàn)。這里先簡單的說說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的時(shí)候會(huì)用到下面的布局文件: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("查詢");   //通過反射,修改默認(rèn)的樣式,可以從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需要的數(shù)據(jù) 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; }}

實(shí)現(xiàn)的效果如下:

以上就是搜索框組件SearchView的基本使用方法,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临城县| 兴山县| 井陉县| 东城区| 蒙城县| 井冈山市| 玉田县| 日喀则市| 徐州市| 景德镇市| 沧州市| 保靖县| 民丰县| 平舆县| 石林| 出国| 伊吾县| 安吉县| 珲春市| 内丘县| 文化| 忻城县| 读书| 喀喇沁旗| 措美县| 宁城县| 普宁市| 南华县| 奈曼旗| 郧西县| 商水县| 衡阳市| 佛山市| 隆安县| 伊春市| 凤翔县| 双峰县| 尼勒克县| 易门县| 巴林左旗| 厦门市|