//安卓的编辑框输入2个字母实现自动补充文本的效果
public class MainActivity extends Activity {
private static final String[] cities=new String[]
{"FuZhou", "XiaMen", "NiDe", "PuTian","QuanZhou", "ZhangZhou", "LongYan", "SanMing","NanPing"};
private AutoCompleteTextView autoCompleteTextView=null;
private MultiAutoCompleteTextView multiAutoCompleteTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
multiAutoCompleteTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
cities);
autoCompleteTextView.setAdapter(adapter);
//设置输入多少字符后提示,默认值为2
autoCompleteTextView.setThreshold(2);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setThreshold(2);
//用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
public class MainActivity extends Activity {
private static final String[] cities=new String[]
{"FuZhou", "XiaMen", "NiDe", "PuTian","QuanZhou", "ZhangZhou", "LongYan", "SanMing","NanPing"};
private AutoCompleteTextView autoCompleteTextView=null;
private MultiAutoCompleteTextView multiAutoCompleteTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
multiAutoCompleteTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
cities);
autoCompleteTextView.setAdapter(adapter);
//设置输入多少字符后提示,默认值为2
autoCompleteTextView.setThreshold(2);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setThreshold(2);
//用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
//布局layout的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>