国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android高級組件AutoCompleteTextView自動完成文本框使用詳解

2019-10-22 18:18:27
字體:
來源:轉載
供稿:網友

自動完成文本框(AutoCompleteTextView),用于實現允許用戶輸入一定字符后,顯示一個下拉菜單,供用戶從中選擇,當用戶選擇某個選項之后,按用戶選擇自動填寫該文本框。

語法格式:

<AutoCompleteTextView
屬性列表>
</AutoCompleteTextView>

AutoCompleteTextView組件繼承EditText,所以它支持EditText組件提供的屬性,同時,該組件還有以下屬性:
android:completionHint 下拉列表下面的說明性文字
android:completionThreshold 彈出下來列表的最小字符個數
android:dropDownAnchor 下拉列表的錨點或掛載點
android:dropDownHeight 下拉列表高度
android:dropDownWidth 下拉列表寬度
android:dropDownHorizontalOffset 下拉列表距離左邊的距離
android:dropDownVerticalOffset       下拉列表距離上邊的距離
android:dropDownSelector 下拉列表被選中的行的背景
android:popupBackground         下拉列表的背景

下面實現帶自動提示功能的搜索框:

效果如圖所示:

Android,自動完成文本框,AutoCompleteTextView

具體實現:

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="輸入搜索內容"     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> 

界面效果如圖:

Android,自動完成文本框,AutoCompleteTextView

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 {   //此字符串是要在下拉菜單中顯示的列表項   private static final String[] COUNTRIES=new String[]{"HeNan","HeNan大學",     "HeNan理工大學","HeNan工業大學","HeNan萬方科技學院","HeNan第二附屬醫院二院"};   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);          //獲取自動完成文本框     final AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);     //注意ArrayAdapter與SimpleAdapter的區別     //創建一個ArrayAdapter適配器     ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);     textView.setAdapter(adapter);//為自動完成文本框設置適配器          Button button=(Button)findViewById(R.id.button0);//獲取"搜索"按鈕     //為搜索按鈕添加事件監聽器     button.setOnClickListener(new OnClickListener() {              @Override       public void onClick(View arg0) {         Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show();                }     });   } } 

實現了自動補全,當打HeNan的時候,下面就會有補全信息。此功能在搜索應用上使用的比較廣泛。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁强县| 内丘县| 苏尼特右旗| 大宁县| 宁南县| 广丰县| 炉霍县| 嘉祥县| 攀枝花市| 瓮安县| 长子县| 桂东县| 镇平县| 新乡市| 常熟市| 东光县| 安多县| 肥城市| 中西区| 科尔| 康平县| 浪卡子县| 辽宁省| 禹城市| 团风县| 海口市| 旬邑县| 梁平县| 昭觉县| 石家庄市| 泰兴市| 杭锦后旗| 砀山县| 锡林郭勒盟| 兴和县| 井冈山市| 黄大仙区| 永春县| 淅川县| 宁武县| 闵行区|