手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android自动文本框输入识别提示功能代码
Android自动文本框输入识别提示功能代码
摘要:自动提示文本框(AutoCompleteTextView)可以加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果)。相信大家都熟悉自...

自动提示文本框(AutoCompleteTextView)可以加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果)。

相信大家都熟悉自动识别提示吧,在我们的生活中随处可见,今天就让我为大家简单介绍一下它是如何设计的。

所谓自动识别输入即是根据用户输入的已有信息,为用户提示可能的值,方便用户完成输入。在Android设备上这种功能分为:AutoCompleteTextView和MultiAutoCompleteTextView,前者为单个的自动识别,类似与搜索引擎的输入框提示;后者为多个值自动识别,类似与发邮件时的邮箱输入框。那它们俩到底如何使用呢?下面就让我们一起学习一下吧。

首先是布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Activityfive" > <AutoCompleteTextView android:id="@+id/acTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名:" android:textColor="#000" android:maxLength="10" /> <MultiAutoCompleteTextView android:id="@+id/macTextView" android:layout_below="@id/acTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入城市:" android:textColor="#000" android:maxLength="20" /> </RelativeLayout>

注:android:hint属性为提示文字内容,当如何输入框获得焦点后自动消失

下面是我们的Action:

import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; public class Activityfive extends Activity{ private AutoCompleteTextView acTextView; private MultiAutoCompleteTextView macTextView; private String [] arr = {"abc","abx","abo","bdc","bdf"}; private String [] brr = {"ab北京","ab南京","ab东京","bb莫斯科","bb英国","bb美国"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_five); acTextView = (AutoCompleteTextView) findViewById(R.id.acTextView); macTextView = (MultiAutoCompleteTextView) findViewById(R.id.macTextView); ArrayAdapter<String> arrAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, arr); acTextView.setAdapter(arrAdapt); ArrayAdapter<String> brrAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, brr); macTextView.setAdapter(brrAdapt); macTextView.setThreshold(1);//设置输入多少个字符开始自动匹配 macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//设置分隔符 } }

以上所述是小编给大家介绍的Android自动文本框输入识别提示功能代码,希望对大家有所帮助,如果大家有任何疑问请给我们留言,小编会及时回复大家的。在此也非常感谢大家对查字典教程网的支持!

【Android自动文本框输入识别提示功能代码】相关文章:

Android判断SDK版本及判断是否联网

android 设置圆角图片实现代码

Android文件下载进度条的实现代码

Android 软件自动更新功能实现的方法

Android图片翻转动画简易实现代码

Android读取用户号码,手机串号,SIM卡序列号的实现代码

android根据分辨率自动调整字体大小的实例代码

Android开发常用小功能

Android 显示和隐藏输入法实现代码

Android获取本机电话号码的简单方法

精品推荐
分类导航