Android搜索框SearchView屬性和用法詳解
SearchView簡(jiǎn)介
SearchView是Android原生的搜索框控件,它提供了一個(gè)用戶界面,用于用戶搜索查詢。
SearchView默認(rèn)是展示一個(gè)search的icon,點(diǎn)擊icon展開搜索框,如果你想讓搜索框默認(rèn)就展開,可以通過setIconifiedByDefault(false);實(shí)現(xiàn)。
SearchView屬性
SearchView使用
xml中定義SearchView:
<?xml version="1.0" encoding="utf-8"?> <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:layout_margin="15dp" android:orientation="vertical" tools:context="com.airsaid.searchviewdemo.MainActivity"> <SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="請(qǐng)輸入搜索內(nèi)容" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
Main代碼:
public class MainActivity extends AppCompatActivity { private String[] mStrs = {"aaa", "bbb", "ccc", "airsaid"}; private SearchView mSearchView; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSearchView = (SearchView) findViewById(R.id.searchView); mListView = (ListView) findViewById(R.id.listView); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrs)); mListView.setTextFilterEnabled(true); // 設(shè)置搜索文本監(jiān)聽 mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { // 當(dāng)點(diǎn)擊搜索按鈕時(shí)觸發(fā)該方法 @Override public boolean onQueryTextSubmit(String query) { return false; } // 當(dāng)搜索內(nèi)容改變時(shí)觸發(fā)該方法 @Override public boolean onQueryTextChange(String newText) { if (!TextUtils.isEmpty(newText)){ mListView.setFilterText(newText); }else{ mListView.clearTextFilter(); } return false; } }); } }
效果截圖:
Demo下載:Android搜索框
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04新版Flutter集成到已有Android項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了新版Flutter集成到已有Android項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充
本篇文章小編為大家介紹,基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充。需要的朋友參考下2013-04-04Android 使用AsyncTask實(shí)現(xiàn)多任務(wù)多線程斷點(diǎn)續(xù)傳下載
這篇文章主要介紹了Android 使用AsyncTask實(shí)現(xiàn)多任務(wù)多線程斷點(diǎn)續(xù)傳下載的相關(guān)資料,需要的朋友可以參考下2018-05-05Android中區(qū)別Drawable Bitmap Canvas Paint
本文主要介紹Android中Drawable Bitmap Canvas Paint 之間的區(qū)別,這里對(duì)這幾個(gè)概念做出詳細(xì)介紹,開發(fā)Android游戲的朋友可以參考下2016-07-07