Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
本文實(shí)例講述了Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:
現(xiàn)在我們上網(wǎng)幾乎都會(huì)用百度或者谷歌搜索信息,當(dāng)我們?cè)谳斎肟蚶镙斎胍粌蓚€(gè)字后,就會(huì)自動(dòng)提示我們想要的信息,這種效果在Android 里是如何實(shí)現(xiàn)的呢? 事實(shí)上,Android 的AutoCompleteTextView Widget ,只要搭配ArrayAdapter 就能設(shè)計(jì)同類(lèi)似Google 搜索提示的效果.
本例子先在Layout 當(dāng)中布局一個(gè)AutoCompleteTextView Widget ,然后通過(guò)預(yù)先設(shè)置好的字符串?dāng)?shù)組,將此字符串?dāng)?shù)組放入ArrayAdapter ,最后利用AutoCompleteTextView.setAdapter 方法,就可以讓AutoCompleteTextView 具有自動(dòng)提示的功能.例如,只要輸入ab ,就會(huì)自動(dòng)帶出包含ab 的所有字符串列表.
讓我們看一下效果圖:
下面是我們程序所涉及變動(dòng)的代碼(本例子代碼寫(xiě)的相對(duì)較少):
首先是main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please input:" /> <AutoCompleteTextView android:id="@+id/actv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
其次是主控制程序AutoCompleteTextViewDemo.Java:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteTextViewDemo extends Activity { private AutoCompleteTextView actv; private static final String[] autoStrs = new String[]{"a","abc","abcd","abcde","ba"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過(guò)findViewById()方法取到actv actv = (AutoCompleteTextView)findViewById(R.id.actv); //new ArrayAdapter對(duì)象并將autoStr字符串?dāng)?shù)組傳入actv中 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStrs); actv.setAdapter(adapter); } }
所有程序就這么一點(diǎn)點(diǎn)哦,大功就這么告成了,最后執(zhí)行之,將達(dá)到上述效果。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- android實(shí)現(xiàn)彈出提示框
- Android仿QQ、微信聊天界面長(zhǎng)按提示框效果
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android仿IOS自定義AlertDialog提示框
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶(hù)端進(jìn)度提示框
- Android模仿Toast實(shí)現(xiàn)提示框效果
相關(guān)文章
Android Studio添加第三方庫(kù)的注意事項(xiàng)
這篇文章給大家介紹的是Android Studio添加第三方庫(kù)遇到的一些坑,以及對(duì)應(yīng)的解決辦法,有需要的可以參考借鑒。2016-09-09Android數(shù)據(jù)持久化之讀寫(xiě)SD卡中內(nèi)容的方法詳解
這篇文章主要介紹了Android數(shù)據(jù)持久化之讀寫(xiě)SD卡中內(nèi)容的方法,結(jié)合具體實(shí)例形式分析了Android持久化操作中針對(duì)SD卡進(jìn)行讀寫(xiě)操作的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-05-05Android評(píng)分控件RatingBar使用實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Android評(píng)分控件RatingBar使用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android貝塞爾曲線(xiàn)實(shí)現(xiàn)加入購(gòu)物車(chē)拋物線(xiàn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線(xiàn)實(shí)現(xiàn)加入購(gòu)物車(chē)拋物線(xiàn)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼
這篇文章主要介紹了獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08Android App中使用LinearLayout進(jìn)行居中布局的實(shí)例講解
這篇文章主要介紹了Android App中使用LinearLayout進(jìn)行居中布局的實(shí)例講解,文中分別介紹了水平居中和垂直居中的相關(guān)線(xiàn)性布局,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)多級(jí)列表中的新建功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)列表中的新建功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法
這篇文章主要介紹了Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法的相關(guān)資料,需要的朋友可以參考下2017-07-07