Android SearchView搜索框組件的使用方法
SearchView是搜索框組件,它可以讓用戶在文本框里輸入文字,通過監(jiān)聽器取得用戶的輸入,當(dāng)用戶點(diǎn)擊搜索時(shí),監(jiān)聽器執(zhí)行實(shí)際的搜索。
本文就為大家分享了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的基本使用方法 進(jìn)行深入學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡書動(dòng)態(tài)searchview搜索欄效果
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- Android搜索框SearchView屬性和用法詳解
- Android遍歷所有文件夾和子目錄搜索文件
- Android搜索框組件SearchView的基本使用方法
- Android ListView用EditText實(shí)現(xiàn)搜索功能效果
- Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能
相關(guān)文章
詳解android使用ItemDecoration 懸浮導(dǎo)航欄效果
本篇文章主要介紹了Android 最流行的吸頂效果的實(shí)現(xiàn)及代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01
Android 集成 google 登錄并獲取性別等隱私信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 集成 google 登錄并獲取 性別等隱私信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
flutter實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
詳解android 用webview加載網(wǎng)頁(https和http)
這篇文章主要介紹了詳解android 用webview加載網(wǎng)頁(https和http),詳細(xì)的介紹了兩個(gè)錯(cuò)誤的解決方法,有興趣的可以了解一下2017-11-11
android 動(dòng)態(tài)控制狀態(tài)欄顯示和隱藏的方法實(shí)例
這篇文章主要介紹了2013-12-12
Android實(shí)現(xiàn)自由拖動(dòng)并顯示文字的懸浮框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自由拖動(dòng)并顯示文字的懸浮框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
修改Android Studio 的 Logcat 緩沖區(qū)大小操作
這篇文章主要介紹了修改Android Studio 的 Logcat 緩沖區(qū)大小操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

