标签:
简介
在Android中提供了两种智能输入框,它们是AutoCompleteTextView、MultiAutoCompleteTextView。它们的功能大致一样,AutoCompleteTextView是从第一个字符就开始联想,而MultiAutoCompleteTextView则可以从指定字符开始联想。MultiAutoCompleteTextView能够对用户键入的文本进行有效地扩充提示,而不需要用户输入整个内容。(用户输入一部分内容,剩下的部分系统就会给予提示)。使用时,必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。布局文件中常用属性
- android:completionHint 设置出现在下拉菜单中的提示标题
- android:completionThreshold 设置用户至少输入多少个字符才会显示提示
- android:dropDownHorizontalOffset 下拉菜单于文本框之间的水平偏移。默认与文本框左对齐
- android:dropDownHeight 下拉菜单的高度
- android:dropDownWidth 下拉菜单的宽度
- android:dropDownVerticalOffset 垂直偏移量
代码-Activity
public class MainActivity extends Activity {private AutoCompleteTextView auto_name;private AutoCompleteTextView auto_mail;private MultiAutoCompleteTextView multi_auto_name;private MultiAutoCompleteTextView multi_auto_mail;private MultiAutoCompleteTextView multi_auto_mail2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);auto_name = (AutoCompleteTextView) findViewById(R.id.auto_name);auto_mail = (AutoCompleteTextView) findViewById(R.id.auto_mail);multi_auto_name = (MultiAutoCompleteTextView) findViewById(R.id.multi_auto_name);multi_auto_mail = (MultiAutoCompleteTextView) findViewById(R.id.multi_auto_mail);multi_auto_mail2 = (MultiAutoCompleteTextView) findViewById(R.id.multi_auto_mail2);//提示框列表的适配器String[] string_names = getResources().getStringArray(R.array.strings);String[] mail_paths = new String[] { "@163.com", "@qq.com", "@sina.com", "@hotmail.com", "@gmail.com" };ArrayAdapter<String> adapterName = new ArrayAdapter<String>(this, R.layout.item_auto_textview, string_names);ArrayAdapter<String> adapterMail = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, mail_paths);auto_mail.setAdapter(adapterName);auto_name.setAdapter(adapterMail);auto_name.setThreshold(1);//设置输入多少字符后提示,默认值为2个,也可通过android:completionThreshold设置//对于MultiAutoCompleteTextView,用户必须通过setTokenizer设置一个Tokenizer用来区分不同的子串multi_auto_name.setTokenizer(new CommaTokenizer());multi_auto_name.setAdapter(adapterName);multi_auto_mail.setTokenizer(new CommaTokenizer());multi_auto_mail.setAdapter(adapterMail);multi_auto_mail2.setTokenizer(new MailBoxAssociateTokenizer());multi_auto_mail2.setAdapter(adapterMail);}}
代码-自定义View
/*** 自定义的MultiAutoCompleteTextView,实现只要输入到@符,就会开始联想邮箱*/public class MailBoxAssociateView extends MultiAutoCompleteTextView {public MailBoxAssociateView(Context context) {super(context);}public MailBoxAssociateView(Context context, AttributeSet attrs) {super(context, attrs);}public MailBoxAssociateView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic boolean enoughToFilter() {// 如果字符中包含‘@‘并且不在第一位,则满足条件return getText().toString().contains("@") && getText().toString().indexOf("@") > 0;}}
代码-自定义分析器
/*** 自定义的MultiAutoCompleteTextView的分析器,指定从哪个字符开始联想*/public class MailBoxAssociateTokenizer implements Tokenizer {@Overridepublic int findTokenEnd(CharSequence text, int cursor) {int i = cursor;int len = text.length();while (i < len) {if (text.charAt(i) == ‘@‘) {return i;} else {i++;}}return len;}@Overridepublic int findTokenStart(CharSequence text, int cursor) {int index = text.toString().indexOf("@");if (index < 0) {index = text.length();}if (index >= findTokenEnd(text, cursor)) {index = 0;}return index;}@Overridepublic CharSequence terminateToken(CharSequence text) {int i = text.length();while (i > 0 && text.charAt(i - 1) == ‘ ‘) {i--;}if (i > 0 && text.charAt(i - 1) == ‘@‘) {return text;} else {if (text instanceof Spanned) {SpannableString sp = new SpannableString(text);TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);return sp;} else {return text;}}}}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><AutoCompleteTextViewandroid:id="@+id/auto_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="AutoCompleteTextView,老白"android:textColor="#f00"android:textSize="16sp" /><AutoCompleteTextViewandroid:id="@+id/auto_mail"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="AutoCompleteTextView,邮箱"android:textSize="16sp" /><MultiAutoCompleteTextViewandroid:id="@+id/multi_auto_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="MultiAutoCompleteTextView,老白"android:textSize="16sp" /><MultiAutoCompleteTextViewandroid:id="@+id/multi_auto_mail"android:layout_width="match_parent"android:layout_height="wrap_content"android:completionThreshold="1"android:hint="MultiAutoCompleteTextView,邮箱"android:textSize="16sp" /><com.bqt.autotv.MailBoxAssociateViewandroid:id="@+id/multi_auto_mail2"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="自定义的MailBoxAssociateView,非常好用"android:textSize="16sp" /></LinearLayout>
AutoCompleteTextView 自动完成 自动邮件检测
标签:
原文地址:http://www.cnblogs.com/baiqiantao/p/5414906.html