自動完成文本框(AutoCompleteTextView),用于實(shí)現(xiàn)允許用戶輸入一定字符后,顯示一個(gè)下拉菜單,供用戶從中選擇,當(dāng)用戶選擇某個(gè)選項(xiàng)之后,按用戶選擇自動填寫該文本框。
10年積累的網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有鶴山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
語法格式:
<AutoCompleteTextView
屬性列表>
</AutoCompleteTextView>
AutoCompleteTextView組件繼承EditText,所以它支持EditText組件提供的屬性,同時(shí),該組件還有以下屬性:
android:completionHint 下拉列表下面的說明性文字
android:completionThreshold 彈出下來列表的最小字符個(gè)數(shù)
android:dropDownAnchor 下拉列表的錨點(diǎn)或掛載點(diǎn)
android:dropDownHeight 下拉列表高度
android:dropDownWidth 下拉列表寬度
android:dropDownHorizontalOffset 下拉列表距離左邊的距離
android:dropDownVerticalOffset 下拉列表距離上邊的距離
android:dropDownSelector 下拉列表被選中的行的背景
android:popupBackground 下拉列表的背景
下面實(shí)現(xiàn)帶自動提示功能的搜索框:
效果如圖所示:
具體實(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萬方科技學(xué)院","HeNan第二附屬醫(yī)院二院"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取自動完成文本框 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);//為自動完成文本框設(shè)置適配器 Button button=(Button)findViewById(R.id.button0);//獲取"搜索"按鈕 //為搜索按鈕添加事件監(jiān)聽器 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show(); } }); } }
實(shí)現(xiàn)了自動補(bǔ)全,當(dāng)打HeNan的時(shí)候,下面就會有補(bǔ)全信息。此功能在搜索應(yīng)用上使用的比較廣泛。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:Android高級組件AutoCompleteTextView自動完成文本框使用詳解
網(wǎng)站URL:http://sd-ha.com/article24/gcjjce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計(jì)
聲明:本網(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)