·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android自动文本框输入识别提示功能代码

Android自动文本框输入识别提示功能代码

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

自动提示文本框(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自动文本框输入识别提示功能代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!