设为首页 加入收藏

TOP

Android之自动文本输入识别提示
2015-02-25 16:15:24 来源: 作者: 【 】 浏览:32
Tags: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" >
? ? ? ? ? ? android:id="@+id/acTextView"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:hint="请输入姓名:"
? ? ? ? android:textColor="#000"
? ? ? ? android:maxLength="10"
? ? ? ? />
? ? ? ? ? ? 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"
? ? ? ? />


注: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 arrAdapt = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, arr);
? ? ? ? acTextView.setAdapter(arrAdapt);
? ? ? ? ArrayAdapter brrAdapt = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, brr);
? ? ? ? macTextView.setAdapter(brrAdapt);
? ? ? ? macTextView.setThreshold(1);//设置输入多少个字符开始自动匹配
? ? ? ? macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//设置分隔符
? ? }
}


代码很简单,没有什么深奥的地方,相信大家一看就可以理解,对于一些不好理解的地方,我也已经添加了注释,希望对你有用。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android属性动画之ObjectAnimator 下一篇Android 属性动画(Property Anima..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: