歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之自動文本輸入識別提示

Android之自動文本輸入識別提示

日期:2017/3/1 9:33:06   编辑:Linux編程

相信大家都熟悉自動識別提示吧,在我們的生活中隨處可見,今天就讓我為大家簡單介紹一下它是如何設計的。

所謂自動識別輸入即是根據用戶輸入的已有信息,為用戶提示可能的值,方便用戶完成輸入。在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 4.4.4 發布下載 http://www.linuxidc.com/Linux/2014-06/103467.htm

最簡單的Ubuntu Touch & Android 雙系統安裝方式 http://www.linuxidc.com/Linux/2014-01/94881.htm

在Nexus上實現Ubuntu和Android 4.4.2 雙啟動 http://www.linuxidc.com/Linux/2014-05/101849.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved