Android 自學之自動完成文字框 AutoCompleteTextView

來源:互聯網
上載者:User

標籤:des   android   style   c   class   blog   

自動完成文字框(AutoCompleteTextView)從EditText派生而出,實際上他也是一個編輯框,但他比普通的編輯框多了一個功能:當使用者輸入一定字元後,自動完成文字框會顯示一個下拉式功能表,供使用者從中選擇,當使用者選擇了某個功能表項目過後,AutoCompleteTextView就會按使用者選擇自動填寫該文字框。

AutoCompleteTextView支援的常用的XML屬性和相關方法及說明

XML屬性 相關方法 說明
android:completionHint setCompletionHint(CharSequence) 設定出現在下拉式功能表中的提示標題
android:completionThreshold setThreshold(int) 設定使用者至少輸入幾個字福才會顯示提示
android:dropDownHeight setDropDownHeight(int) 設定下拉式功能表的高度
android:dropDownHorizontalOffset   設定下拉式功能表與文字框之間的水平位移,下拉式功能表預設與文字框靠左對齊
android:dropDownVerticalOffset   設定下拉式功能表與文字框之間的垂直位移,下拉式功能表預設緊跟文字框
android:dropDownWidth setdropDownWidth(int) 設定下拉式功能表的寬度
android:popupBackground setDropDownBackgroundResource(int) 設定下拉式功能表的背景

 

 

 

 

 

 

AutoCompleteTextView的使用其實很簡單,只要為他設定一個Adapter,該Adapter封裝了AutoCompleteTextView的預設提示文本,下面例舉一個介面布局檔案中包含了AutoCompleteTextView。

layout/main.xml:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="fill_parent" 5     android:layout_height="fill_parent" 6     > 7 <!-- 定義一個自動完成文字框,指定輸入一個字元後進行提示 --> 8 <AutoCompleteTextView   9     android:id="@+id/auto"10     android:layout_width="fill_parent" 11     android:layout_height="wrap_content" 12     android:completionHint="請選擇您喜歡的人:"13     android:dropDownHorizontalOffset="20dp"14     android:completionThreshold="1"15     />16 </LinearLayout>

com.example.autocomplete.AutoCompleteTextViewTest.java

 1 package com.example.autocomplete; 2  3 import android.support.v7.app.ActionBarActivity; 4 import android.support.v7.app.ActionBar; 5 import android.support.v4.app.Fragment; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.Menu; 9 import android.view.MenuItem;10 import android.view.View;11 import android.view.ViewGroup;12 import android.widget.ArrayAdapter;13 import android.widget.AutoCompleteTextView;14 import android.os.Build;15 16 public class AutoCompleteTextViewTest extends ActionBarActivity {17 18     //定義字串數組,作為提示的文本19     String[] books = new String[]{20         "yangjing",21         "yangsen",22         "yanghua",23     };24     25     @Override26     protected void onCreate(Bundle savedInstanceState) {27         super.onCreate(savedInstanceState);28         setContentView(R.layout.main);29         //建立一個ArrayAdapter,封裝數組30         ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, books);31         32         AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.auto);33                 34         auto.setAdapter(arr);35     }36 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.