最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年05月25日 14:10:12   作者:summerpxy  
這篇文章主要為大家詳細(xì)介紹了Android搜索框組件SearchView的基本使用方法,感興趣的小伙伴們可以參考一下

SearchView是android系統(tǒng)中內(nèi)置的一個(gè)搜索框組件,可以很方便在添加在用戶界面之上,但是也帶來(lái)了一些問(wèn)題,那就是searchview的UI是固定的,定制起來(lái)會(huì)很麻煩,如果對(duì)SearchView的要求比較高,完全可以采用button和EditText自己實(shí)現(xiàn)。這里先簡(jiǎn)單的說(shuō)說(shuō)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("查詢");

   //通過(guò)反射,修改默認(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

左云县| 酒泉市| 正蓝旗| 任丘市| 昭平县| 云龙县| 镇雄县| 丰原市| 昌图县| 安乡县| 黄陵县| 象州县| 鹤峰县| 阳朔县| 始兴县| 威信县| 太湖县| 肇州县| 商南县| 缙云县| 和林格尔县| 甘谷县| 西宁市| 甘孜县| 大城县| 新营市| 当涂县| 永宁县| 金堂县| 湾仔区| 赤峰市| 碌曲县| 大悟县| 德钦县| 兴和县| 苏州市| 华坪县| 镇安县| 泾阳县| 额济纳旗| 全椒县|