Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用示例
本文實例講述了Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應用。分享給大家供大家參考,具體如下:
現在我們上網幾乎都會用百度或者谷歌搜索信息,當我們在輸入框里輸入一兩個字后,就會自動提示我們想要的信息,這種效果在Android 里是如何實現的呢? 事實上,Android 的AutoCompleteTextView Widget ,只要搭配ArrayAdapter 就能設計同類似Google 搜索提示的效果.
本例子先在Layout 當中布局一個AutoCompleteTextView Widget ,然后通過預先設置好的字符串數組,將此字符串數組放入ArrayAdapter ,最后利用AutoCompleteTextView.setAdapter 方法,就可以讓AutoCompleteTextView 具有自動提示的功能.例如,只要輸入ab ,就會自動帶出包含ab 的所有字符串列表.
讓我們看一下效果圖:

下面是我們程序所涉及變動的代碼(本例子代碼寫的相對較少):
首先是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);
//通過findViewById()方法取到actv
actv = (AutoCompleteTextView)findViewById(R.id.actv);
//new ArrayAdapter對象并將autoStr字符串數組傳入actv中
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,autoStrs);
actv.setAdapter(adapter);
}
}
所有程序就這么一點點哦,大功就這么告成了,最后執(zhí)行之,將達到上述效果。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
android項目從Eclipse遷移到Android studio中常見問題解決方法
android項目從Eclipse遷移到Android studio中經常會遇到一些問題,本文提供了Android studio使用中常見問題解決方法2018-03-03
Android穩(wěn)定性:可遠程配置化的Looper兜底框架
這篇文章主要為大家介紹了Android穩(wěn)定性可遠程配置化的Looper兜底框架實例實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android中MPAndroidChart自定義繪制最高點標識的方法
目前在做一款軟件,要求在展示走勢圖的時候對最高點進行自定義繪制,下面這篇文章主要給大家介紹了關于Android中MPAndroidChart自定義繪制最高點標識的方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2018-03-03
Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解
本篇文章介紹了,Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解。需要的朋友參考下2013-04-04
flutter監(jiān)聽app進入前后臺狀態(tài)的實現
在開發(fā)app的過程中,我們經常需要知道app處于前后臺的狀態(tài),本文主要介紹了flutter監(jiān)聽app進入前后臺狀態(tài)的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

