Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView

时间:2024-11-27 09:02:56

AutoCompleteTextView: 就是一个带自动提示的EditText,当输入字符时,会出现提示.
android:completionThreshold 
输入几个字符时提示
android:completionHint/setCompletionHint(CharSequence)
设置出现下拉列表的提示标题
android:dropDownHeight/setDropHeight(int)
设置下拉列表的高度
android:dropDownWidth/setDropWidth(int)
设置下拉列表的宽度
android:popupBackground/setDropDownbackgroundResource(int)
设置下拉列表的背景
在Android程序中,为了展示数据,通常会用到一个Adapter的接口
setAdapter()设置提示内容的Adapter.

MultiAutoCompleteTextView是多选选项
使用MultiAutoCompleteTextView必须实现MultiAutoCompleteTextView.Tokenizer接口
用于声明用于选项与选项之间分隔的符号,一般如不特殊指定,可以使用Android为我们提供的实现类MultiAutoCompleteTextView.CommaTokenizer,它设定使用英文逗号","进行分隔选项.

例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选搜索"
tools:ignore="HardcodedText" /> <AutoCompleteTextView android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/> </LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选搜索"
tools:ignore="HardcodedText" /> <MultiAutoCompleteTextView android:id="@+id/autoMultiText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> </LinearLayout>
package com.light.android.study;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.MultiAutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView.CommaTokenizer;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton; public class MainActivity extends Activity { private AutoCompleteTextView autoText;
private MultiAutoCompleteTextView autoMultiText;
private String[] keywords = {
"ab","abc","abdfdf","abnsd","abdfff"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initAdapter();
} private void init(){
autoText = (AutoCompleteTextView) findViewById(R.id.autoText);
autoMultiText = (MultiAutoCompleteTextView) findViewById(R.id.autoMultiText);
} private void initAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, keywords);
autoText.setAdapter(adapter);
autoMultiText.setAdapter(adapter);
autoMultiText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
} }

效果:

Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView

Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView