SearchView屬性如何在Android應(yīng)用中使用?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的通州網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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="請輸入搜索內(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; } }); } }
關(guān)于SearchView屬性如何在Android應(yīng)用中使用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
文章題目:SearchView屬性如何在Android應(yīng)用中使用
當(dāng)前網(wǎng)址:http://chinadenli.net/article44/gedehe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站改版、網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)、ChatGPT、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)