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

首頁 > 系統 > Android > 正文

Android開發高級組件之自動完成文本框(AutoCompleteTextView)用法示例【附源碼下載】

2019-12-12 01:20:57
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發高級組件之自動完成文本框(AutoCompleteTextView)用法。分享給大家供大家參考,具體如下:

通常來說自動完成文本框(AutoCompleteTextView)從EditText派生而出,實際上他也是一個編輯框,但他比普通的編輯框多了一個功能:當用戶輸入一定字符后,自動完成文本框會顯示一個下拉菜單,供用戶從中選擇,當用戶選擇了某個菜單項過后,AutoCompleteTextView就會按用戶選擇自動填寫該文本框。

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

語法格式:

<AutoCompleteTextView屬性列表></AutoCompleteTextView>

AutoCompleteTextView組件繼承EditText,所以它支持EditText組件提供的屬性,同時,該組件還有以下屬性:

屬性 功能
android:completionHint 下拉列表下面的說明性文字
android:completionThreshold 彈出下來列表的最小字符個數
android:dropDownAnchor 下拉列表的錨點或掛載點
android:dropDownHeight 下拉列表高度
android:dropDownWidth 下拉列表寬度
android:dropDownHorizontalOffset 下拉列表距離左邊的距離
android:dropDownVerticalOffset 下拉列表距離上邊的距離
android:dropDownSelector 下拉列表被選中的行的背景
android:popupBackground 下拉列表的背景

效果如下所示:

具體實現步驟:

界面布局 res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal"  android:background="#000000"><AutoCompleteTextView  android:layout_height="wrap_content"  android:text=""  android:id="@+id/autoCompleteTextView1"  android:completionThreshold="2"  android:completionHint="請輸入內容"  android:background="#333333"   android:layout_marginLeft="10dp"  android:layout_weight="7"  android:layout_width="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="10dp"/></LinearLayout>

MainActivity.java文件中:

首先設置保存下拉菜單列表項內容:

//此字符串是要在下拉菜單中顯示的列表項private static final String[] COUNTRIES=new String[]{"jb51","jb51武林網","jb51腳本下載","jb51軟件下載","m.survivalescaperooms.com","武林網"};

onCreate()方法中獲取自動完成文本框,并為自動完成文本框設置適配器,具體實現代碼如下:

//獲取自動完成文本框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.setOnClickListener(new OnClickListener() {  public void onClick(View arg0) {    Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show();  }});

附:完整實例代碼點擊此處本站下載

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄大仙区| 壶关县| 南昌市| 庄河市| 巢湖市| 湾仔区| 龙州县| 辽中县| 新乡市| 嘉义市| 德州市| 乐安县| 汉中市| 两当县| 海晏县| 兰考县| 岳普湖县| 内乡县| 普定县| 中宁县| 清远市| 蒙阴县| 梓潼县| 民乐县| 康马县| 舒兰市| 南平市| 玉溪市| 屏东市| 兴城市| 砚山县| 祥云县| 河池市| 扬中市| 介休市| 永兴县| 保亭| 舟山市| 余江县| 漳州市| 砚山县|