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

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,它设定使用英文逗号","进行分隔选项.

例子:


    
    
    
    

    
    
   
   
    
    
   
  
   

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 adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, keywords);
		autoText.setAdapter(adapter);
		autoMultiText.setAdapter(adapter);
		autoMultiText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
	}
	

}


效果:




 

转载于:https://www.cnblogs.com/krislight1105/p/3748364.html

你可能感兴趣的:(Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView)