Android SearchView搜索框組件的使用方法
SearchView是搜索框組件,它可以讓用戶在文本框里輸入文字,通過監(jiān)聽器取得用戶的輸入,當(dāng)用戶點擊搜索時,監(jiān)聽器執(zhí)行實際的搜索。
本文就為大家分享了SearchView搜索框組件的使用方法,供大家參考,具體內(nèi)容如下
效果:

代碼SearchActivity.java
package com.jialianjia.bzw.activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.annotation.SuppressLint;
import android.widget.ListView;
import android.widget.SearchView;
import com.jialianjia.bzw.BaseActivity;
import com.jialianjia.bzw.R;
import com.lidroid.xutils.ViewUtils;
import java.util.ArrayList;
/**
* 搜索
* Created by Gxs on 2016/5/5.
*/
public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener{
private SearchView searchView;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> arrayList = new ArrayList<String>();
private Object[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
names = loadData();
ViewUtils.inject(this);
searchView = (SearchView) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
android.R.layout.simple_expandable_list_item_1, names));
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(false);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Object[] obj = searchItem(newText);
updateLayout(obj);
return false;
}
public Object[] searchItem(String name) {
ArrayList<String> mSearchList = new ArrayList<String>();
for (int i = 0; i < arrayList.size(); i++) {
int index = arrayList.get(i).indexOf(name);
// 存在匹配的數(shù)據(jù)
if (index != -1) {
mSearchList.add(arrayList.get(i));
}
}
return mSearchList.toArray();
}
// 更新數(shù)據(jù)
public void updateLayout(Object[] obj) {
listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
android.R.layout.simple_expandable_list_item_1, obj));
}
// 測試數(shù)據(jù)
public Object[] loadData() {
arrayList.add("aaa");
arrayList.add("aab");
arrayList.add("aac");
arrayList.add("aad");
arrayList.add("abc");
arrayList.add("abcd");
arrayList.add("cdf");
arrayList.add("eda");
arrayList.add("sdfa");
arrayList.add("ddda");
arrayList.add("sssa");
return arrayList.toArray();
}
}
布局activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="50dp" android:background="?attr/colorPrimary" android:theme="@style/AppTheme.AppBarOverlay" android:fitsSystemWindows="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"> <SearchView android:id="@+id/searchView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:background="@drawable/shape_search" android:queryHint="請輸入您要查找的內(nèi)容"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回"/> </LinearLayout> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="right"></TableRow> <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#969696"/> </LinearLayout>
大家還可以參考:Android搜索框組件SearchView的基本使用方法 進行深入學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
- Android實現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android自定義View實現(xiàn)搜索框(SearchView)功能
- Android仿簡書動態(tài)searchview搜索欄效果
- Android百度地圖實現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點擊時彈出泡泡
- Android搜索框SearchView屬性和用法詳解
- Android遍歷所有文件夾和子目錄搜索文件
- Android搜索框組件SearchView的基本使用方法
- Android ListView用EditText實現(xiàn)搜索功能效果
- Android實現(xiàn)簡單動態(tài)搜索功能
相關(guān)文章
Android使用Shape實現(xiàn)ProgressBar樣式實例
本篇文章主要介紹了Android使用Shape實現(xiàn)ProgressBar樣式實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
使用PackageManager獲得應(yīng)用信息實例方法
PackageManager是Android中一個很有用的類,能夠獲取已安裝的應(yīng)用(包)的信息,如應(yīng)用名稱、圖標(biāo)、權(quán)限,安裝、刪除應(yīng)用(包)等2013-11-11
Android?Studio實現(xiàn)彈窗設(shè)置
這篇文章主要為大家詳細(xì)介紹了Android?Studio實現(xiàn)彈窗設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android RecyclerView 實現(xiàn)快速滾動的示例代碼
本篇文章主要介紹了Android RecyclerView 實現(xiàn)快速滾動的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
Android實現(xiàn)向Launcher添加快捷方式的方法
這篇文章主要介紹了Android實現(xiàn)向Launcher添加快捷方式的方法,涉及Android添加快捷方式的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
Android開發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開發(fā)中會經(jīng)常遇到Activity組件的使用,下面就來講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過本文,你可以進一步了接Android中Activity的運作方式。2015-05-05
Android利用ShaderMask實現(xiàn)花里胡哨的文字特效
我們的?App?大部分時候的文字都是一種顏色,實際上,文字的顏色也可以多姿多彩。我們今天就來介紹一個能夠輕松實現(xiàn)文字漸變色的組件?——?ShaderMask,感興趣的可以了解一下2022-12-12

