Android高級(jí)組件AutoCompleteTextView自動(dòng)完成文本框使用詳解
自動(dòng)完成文本框(AutoCompleteTextView),用于實(shí)現(xiàn)允許用戶輸入一定字符后,顯示一個(gè)下拉菜單,供用戶從中選擇,當(dāng)用戶選擇某個(gè)選項(xiàng)之后,按用戶選擇自動(dòng)填寫(xiě)該文本框。
語(yǔ)法格式:
<AutoCompleteTextView
屬性列表>
</AutoCompleteTextView>
AutoCompleteTextView組件繼承EditText,所以它支持EditText組件提供的屬性,同時(shí),該組件還有以下屬性:
android:completionHint 下拉列表下面的說(shuō)明性文字
android:completionThreshold 彈出下來(lái)列表的最小字符個(gè)數(shù)
android:dropDownAnchor 下拉列表的錨點(diǎn)或掛載點(diǎn)
android:dropDownHeight 下拉列表高度
android:dropDownWidth 下拉列表寬度
android:dropDownHorizontalOffset 下拉列表距離左邊的距離
android:dropDownVerticalOffset 下拉列表距離上邊的距離
android:dropDownSelector 下拉列表被選中的行的背景
android:popupBackground 下拉列表的背景
下面實(shí)現(xiàn)帶自動(dòng)提示功能的搜索框:
效果如圖所示:
具體實(shí)現(xiàn):
res/layout/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:background="#000000"> <AutoCompleteTextView android:layout_width="wrap_content" android:text="" android:id="@+id/autoCompleteTextView1" android:completionThreshold="2" android:completionHint="輸入搜索內(nèi)容" android:background="#FFFFFF" android:layout_marginLeft="10px" android:layout_weight="7" android:layout_height="wrap_content"> </AutoCompleteTextView> <Button android:text="搜索" android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10px"/> </LinearLayout>
界面效果如圖:
MainActivity:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { //此字符串是要在下拉菜單中顯示的列表項(xiàng) private static final String[] COUNTRIES=new String[]{"HeNan","HeNan大學(xué)", "HeNan理工大學(xué)","HeNan工業(yè)大學(xué)","HeNan萬(wàn)方科技學(xué)院","HeNan第二附屬醫(yī)院二院"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取自動(dòng)完成文本框 final AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); //注意ArrayAdapter與SimpleAdapter的區(qū)別 //創(chuàng)建一個(gè)ArrayAdapter適配器 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,COUNTRIES); textView.setAdapter(adapter);//為自動(dòng)完成文本框設(shè)置適配器 Button button=(Button)findViewById(R.id.button0);//獲取"搜索"按鈕 //為搜索按鈕添加事件監(jiān)聽(tīng)器 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show(); } }); } }
實(shí)現(xiàn)了自動(dòng)補(bǔ)全,當(dāng)打HeNan的時(shí)候,下面就會(huì)有補(bǔ)全信息。此功能在搜索應(yīng)用上使用的比較廣泛。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android AutoCompleteTextView控件使用實(shí)例
- 基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充
- 實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件
- Android AutoCompleteTextView連接數(shù)據(jù)庫(kù)自動(dòng)提示的方法(附demo源碼下載)
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- Android自動(dòng)編輯文本框(AutoCompleteTextView)使用方法詳解
- Android中AutoCompleteTextView自動(dòng)提示
- android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
- Android AutoCompleteTextView控件基本用法示例
- Android開(kāi)發(fā)高級(jí)組件之自動(dòng)完成文本框(AutoCompleteTextView)用法示例【附源碼下載】
相關(guān)文章
Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android調(diào)用密碼鎖屏校驗(yàn)的流程代碼詳解
這篇文章主要介紹了Android調(diào)用密碼鎖屏校驗(yàn)的流程代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08解決webview內(nèi)的iframe中的事件不可用的問(wèn)題
這篇文章主要介紹了解決webview內(nèi)的iframe中的事件不可用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼
這篇文章主要介紹了Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果 的相關(guān)資料,需要的朋友可以參考下2016-07-07Android頂部狀態(tài)欄透明化并釋放空間的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android頂部狀態(tài)欄透明化并釋放空間的兩種實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android開(kāi)發(fā)實(shí)現(xiàn)在TextView前面加標(biāo)簽示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)實(shí)現(xiàn)TextView前面加標(biāo)簽示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Android實(shí)現(xiàn)帶圓環(huán)的圓形頭像
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶圓環(huán)的圓形頭像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08