标签:android
AutoComplicatedTextView是从EditText派生出来的,可以使用EditText的所有的属性。当用户输入一定的内容后,会有提示,选中后自动填充。顺便介绍一下MultiAutoComplicatedTextView,和上面的用法基本相似,只是可以输入多个提示项,然后用分隔符隔开,用setTokenize()设置分隔符。
package peng.liu.testview;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private AutoCompleteTextView auto;
private MultiAutoCompleteTextView mauto;
private String[] names = new String[]{"hello","world","java","android"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
auto = (AutoCompleteTextView) findViewById(R.id.auto);
mauto = (MultiAutoCompleteTextView) findViewById(R.id.mauto);
auto.setAdapter(adapter);
mauto.setAdapter(adapter);
mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/auto"
android:completionHint="选择喜欢的图书"
android:dropDownHorizontalOffset="10dp"
android:completionThreshold="1"/>
<MultiAutoCompleteTextView
android:id="@+id/mauto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>
</LinearLayout>
Android的自动完成文本框-android学习之旅(二十六)
标签:android
原文地址:http://blog.csdn.net/lpjishu/article/details/46279163