Android[初级教程]第六章 AutoCompleteTextView和MultiAutoCompleteTextView控件

本文介绍了Android中的AutoCompleteTextView和MultiAutoCompleteTextView控件,它们用于自动补全输入,简化用户输入过程。文章通过实例展示了如何在XML布局文件中定义这些控件,并在Java代码中实现其功能,最后解释了它们在实际应用中的用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章我们来介绍AutoCompleteTextView和MultiAutoCompleteTextView控件,这两个控件都是EditText的子件,有什么用呢?

就拿前几章的西游记里的妖怪抓唐僧师徙来说,妖怪说他们人太多了,我记不清他们的名字了,好像里面有一个是猪九戒的,不对,是猪八戒,两个妖怪争论不休,怎么办?没关系,这就用到我们的自动提示控件了,当输猪字的时候,看八戒名字出来了吧,这样就不需要争论了.简单吧.

但两者有什么区别呢?看名字啊,AutoCompleteTextView控件是每次选一个,一次选一个.MultiAutoCompleteTextView控件呢,就是一次选几个,当然也可以是一个,但一个就用不着这控件啦,呵呵,可以一次多选几个,省点事.好了,我们来看

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="这次妖怪们想抓" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <AutoCompleteTextView android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:hint="请输入您知道的西游记人物" android:completionHint="我知道的人物" android:completionThreshold="1"> </AutoCompleteTextView> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="请输入您知道的西游记人物" android:completionHint="我知道的人物" android:completionThreshold="1"></MultiAutoCompleteTextView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/text"></TextView> </LinearLayout>

定义了两个控件,一个AutoCompleteTextView控件,一个MultiAutoCompleteTextView控件

Activity的java代码如下:

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import android.widget.TextView; public class ButtonDemoActivity extends Activity { private TextView text = null; private String[] item = { "唐僧", "孙悟空 ", "猪八戒", "沙和尚" }; private AutoCompleteTextView autoCompleteTextView; private MultiAutoCompleteTextView multiAutoCompleteTextView; private ArrayAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 通过ID查找到main.xml中的TextView控件 text = (TextView) findViewById(R.id.text); // 通过ID查找到main.xml中的AutoCompleteTextView控件 autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); // 设定一个Array适配器,将数组数据放入适配器中 adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, item); // 对AutoCompleteTextView进行适配 autoCompleteTextView.setAdapter(adapter); //设置AutoCompleteTextView的监听器 autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String str = "这次妖精把" + autoCompleteTextView.getText().toString() + "抓住了!"; updateText(str); } }); // 通过ID查找到main.xml中的MultiAutoCompleteTextView控件 multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView); // 对MultiAutoCompleteTextView进行适配 multiAutoCompleteTextView.setAdapter(adapter); //设置分隔符,默认的是逗号(,) multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); //设置MultiAutoCompleteTextView的监听器 multiAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String str = "这次妖精把" + multiAutoCompleteTextView.getText().toString() + "抓住了!"; updateText(str); } }); } private void updateText(String string) { // 将文本信息设置给TextView控件显示出来 text.setText(string); } }

好了,呵呵,代码都写完,选一个还是选几个随便你啦.图如下:



光说西游记的事了,很多都知道AutoCompleteTextView控件,但MultiAutoCompleteTextView有什么作用呢?

其实你发短信群发的时候就用到MultiAutoCompleteTextView控件啦,不然,你的短信每次都输一遍,多麻烦啊,用MultiAutoCompleteTextView就只需要在收信人后面加号码啦.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值