溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-10-04 15:53:20 來源:腳本之家 閱讀:922 作者:光仔December 欄目:移動開發(fā)

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

語法格式:

<AutoCompleteTextView
屬性列表>
</AutoCompleteTextView>

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

下面實現(xiàn)帶自動提示功能的搜索框:

效果如圖所示:

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

具體實現(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> 

界面效果如圖:

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大學(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)建一個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(); 
         
      } 
    }); 
  } 
} 

實現(xiàn)了自動補全,當(dāng)打HeNan的時候,下面就會有補全信息。此功能在搜索應(yīng)用上使用的比較廣泛。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI