星空网 > 软件开发 > 操作系统

我的android学习经历12

自动匹配输入的内容(文章最后有一个问题有兴趣的可以解答一下,谢谢大神了)

这个主要是两个控件MultiAutoCompleteTextView和AutoCompleteTextView

这两个控件和TextView的主要区别就是可以自动匹配用户输入的内容,就像百度,在百度的搜索框中输入信息时,会提示你一些信息

我的android学习经历12images/loading.gif' data-original="http://images2015.cnblogs.com/blog/927039/201604/927039-20160411210434754-1314665335.png" />

这两个控件的属性主要比TextView多了一个属性   android:completionThreshold="2",这个属性主要是来说明用户输入多少字符时开始匹配(我这里是两个字符);

(1)先讲一下AutoCompleteTextView的实现

布局文件中有关的代码如下(主要的属性大家都认识,绿色的为特有的属性)

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:hint="请输入内容"
android:textColor="#000000"
android:completionThreshold="2"
>
<requestFocus />
</AutoCompleteTextView>

在源代码文件中的代码为

private AutoCompleteTextView auTextView;

String []res={"ab1","ab2","ab3","ab4","ab5"};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auTextView=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
//定义适配器并且添加适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res);
auTextView.setAdapter(adapter);

}

效果图如下:

我的android学习经历12

 

(2)MultiAutoCompleteTextView的实现

它和AutoCompleteTextView的主要区别就在于它可以多次匹配,只要定义了分隔符,而AutoCompleteTextView只可以进行一次匹配

它在布局文件中的代码为

<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/autoCompleteTextView1"
android:layout_marginTop="24dp"
android:textColor="#000000"
android:completionThreshold="2"
android:hint="请输入内容" />

它在代码文件中的代码为:

private MultiAutoCompleteTextView muTextView;
String []res={"ab1","ab2","ab3","ab4","ab5"};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//定义适配器并且添加适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res);

 

muTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
muTextView.setAdapter(adapter);
//设置逗号分隔符
muTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}

效果图如下:

我的android学习经历12

 

 

那个设置分隔符的时候我不太懂我查了一下主要是用到了new MultiAutoCompleteTextView.CommaTokenizer(),不知道为什么这个就是逗号分隔符,希望大神可以解答,谢谢

有什么错误的地方请指出,谢谢

 




原标题:我的android学习经历12

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok色板:https://www.goluckyvip.com/tag/81148.html
tiktok怎么在国内看:https://www.goluckyvip.com/tag/81149.html
tiktok海外国际版官网:https://www.goluckyvip.com/tag/81150.html
外国版tiktok下载:https://www.goluckyvip.com/tag/81151.html
tiktokshop:https://www.goluckyvip.com/tag/81153.html
缅甸tiktok下载:https://www.goluckyvip.com/tag/81155.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
5月贾汪好玩的地方 贾汪哪有好玩的地方:https://www.vstour.cn/a/363179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流