Android搜索框SearchView屬性和用法詳解
SearchView簡介
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搜索框
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android Web跳轉(zhuǎn)到app指定頁面并傳遞參數(shù)實(shí)例
這篇文章主要介紹了android Web跳轉(zhuǎn)到app指定頁面并傳遞參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android 使用 okhttp3和retrofit2 進(jìn)行單文件和多文件上傳
這篇文章主要介紹了Android 使用 okhttp3和retrofit2 進(jìn)行單文件和多文件上傳,開發(fā)項(xiàng)目中需要進(jìn)行單文件多文件的上傳功能,下面演示的ApiResponse是自己分裝的返回值,要根據(jù)自己的項(xiàng)目來完成,需要的朋友可以參考下2022-10-10
一步步實(shí)現(xiàn)Viewpager卡片翻頁效果
一步步實(shí)現(xiàn)Viewpager卡片翻頁效果,文章很精彩,實(shí)現(xiàn)步驟很詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解
系統(tǒng)的啟動(dòng)過程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介
這篇文章主要介紹了Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介,需要的朋友可以參考下2014-10-10

