我们在用百度、淘宝等之类搜索输入一些字符时,会自动弹出一个下拉框提示类似的结果。在Android中提供了两种智能输入框,它们是AutoCompleteTextView、MultiAutoCompleteTextView,今天来学习下。
AutoCompleteTextView、MultiAutoCompleteTextView二者区别是前者选择一个,后者可以选择多个。
直接上代码,首先改下xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:orientation="vertical" 6 android:padding="5dp" > 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="Country_single" /> 12 13 <AutoCompleteTextView 14 android:id="@+id/autocomplete_country" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:layout_marginLeft="5dp" 18 android:completionHint="请选择:" 19 android:completionThreshold="1" 20 android:dropDownHorizontalOffset="10dp" /> 21 22 <TextView 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="Country_multi" /> 26 27 <MultiAutoCompleteTextView 28 android:id="@+id/multi" 29 android:layout_width="fill_parent" 30 android:layout_height="wrap_content" 31 android:layout_marginLeft="5dp" /> 32 33 </LinearLayout>
修改MainActivity.java代码:
1 package com.example.autocompletetextviewdemo; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.AutoCompleteTextView; 7 import android.widget.MultiAutoCompleteTextView; 8 9 public class MainActivity extends Activity { 10 11 public boolean actvflag = true; 12 public String[] countries = null; 13 private MultiAutoCompleteTextView multi=null; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); 21 multi = (MultiAutoCompleteTextView) findViewById(R.id.multi); 22 23 if (actvflag) { 24 countries = new String[] { "Apple", "Android", "asian", 25 "American", "Andorra" }; 26 } else { 27 countries = getResources().getStringArray( 28 R.array.countries_array); 29 } 30 // 配置Adaptor 31 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 32 android.R.layout.simple_dropdown_item_1line, countries); 33 34 autoCompleteTextView.setAdapter(adapter); 35 36 //初始化multi 37 multi.setAdapter(adapter); 38 //设置输入多少字符后提示,默认值为2,在此设为1 39 multi.setThreshold(1); 40 //用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。 41 multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 42 } 43 44 }
再改下value/string.xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">AutoCompleteTextViewDemo</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 8 <string-array name="countries_array"> 9 <item>Babylon</item> 10 <item>background</item> 11 <item>Barbados</item> 12 <item>Bag</item> 13 <item>bacteria</item> 14 <item>Belize</item> 15 <item>baggage</item> 16 </string-array> 17 18 </resources>
运行效果:
点击"AutoCompleteTextView":
点击"MultiAutoCompleteTextView“框:
时间: 2024-10-07 20:01:19