xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".chlidactivity.AutoEdittextActivity">
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<MultiAutoCompleteTextView
android:id="@+id/mauto"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity中代码
package com.example.android_insane.chlidactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import com.example.android_insane.R;
public class AutoEdittextActivity extends AppCompatActivity {
private AutoCompleteTextView auto;
private MultiAutoCompleteTextView mauto;
//定义数组 作为提示文本
String[] books = new String[]{
"疯狂java讲义",
"疯狂Ajax讲义",
"疯狂XML讲义",
"疯狂Workflow讲义"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_edittext);
initView();
initData();
}
private void initData() {
//创建一个ArrayAdapter封装数组
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,books);
auto.setAdapter(aa);
mauto.setAdapter(aa);
mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private void initView() {
auto = (AutoCompleteTextView) findViewById(R.id.auto);
mauto = (MultiAutoCompleteTextView) findViewById(R.id.mauto);
}
}