Android SearchView搜索框組件的使用方法
SearchView是搜索框組件,它可以讓用戶在文本框里輸入文字,通過(guò)監(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)); } // 測(cè)試數(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="請(qǐng)輸入您要查找的內(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í)。
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡(jiǎn)書動(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)簡(jiǎn)單動(dòng)態(tài)搜索功能
相關(guān)文章
Android使用Shape實(shí)現(xiàn)ProgressBar樣式實(shí)例
本篇文章主要介紹了Android使用Shape實(shí)現(xiàn)ProgressBar樣式實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04使用PackageManager獲得應(yīng)用信息實(shí)例方法
PackageManager是Android中一個(gè)很有用的類,能夠獲取已安裝的應(yīng)用(包)的信息,如應(yīng)用名稱、圖標(biāo)、權(quán)限,安裝、刪除應(yīng)用(包)等2013-11-11Android View實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Android?Studio實(shí)現(xiàn)彈窗設(shè)置
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)彈窗設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
本篇文章主要介紹了Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Android實(shí)現(xiàn)向Launcher添加快捷方式的方法
這篇文章主要介紹了Android實(shí)現(xiàn)向Launcher添加快捷方式的方法,涉及Android添加快捷方式的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09Android開發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開發(fā)中會(huì)經(jīng)常遇到Activity組件的使用,下面就來(lái)講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過(guò)本文,你可以進(jìn)一步了接Android中Activity的運(yùn)作方式。2015-05-05Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-04-04Android利用ShaderMask實(shí)現(xiàn)花里胡哨的文字特效
我們的?App?大部分時(shí)候的文字都是一種顏色,實(shí)際上,文字的顏色也可以多姿多彩。我們今天就來(lái)介紹一個(gè)能夠輕松實(shí)現(xiàn)文字漸變色的組件?——?ShaderMask,感興趣的可以了解一下2022-12-12