欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android高級(jí)組件AutoCompleteTextView自動(dòng)完成文本框使用詳解

 更新時(shí)間:2017年12月26日 15:21:06   作者:光仔December  
這篇文章主要介紹了Android高級(jí)組件AutoCompleteTextView自動(dòng)完成文本框的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

語(yǔ)法格式:

<AutoCompleteTextView
屬性列表>
</AutoCompleteTextView>

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

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

效果如圖所示:

具體實(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萬(wàn)方科技學(xué)院","HeNan第二附屬醫(yī)院二院"}; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    //獲取自動(dòng)完成文本框 
    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);//為自動(dòng)完成文本框設(shè)置適配器 
     
    Button button=(Button)findViewById(R.id.button0);//獲取"搜索"按鈕 
    //為搜索按鈕添加事件監(jiān)聽(tīng)器 
    button.setOnClickListener(new OnClickListener() { 
       
      @Override 
      public void onClick(View arg0) { 
        Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show(); 
         
      } 
    }); 
  } 
} 

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論