/**
* 动态匹配的 TextView
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 1.初始化控件
* 2.创建一个适配器
* 3.绑定适配器
*/
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.auto_complete_textView);
String[] strings = {"beijing","beijing321", "beijing_tian_an_men", "aomen", "ao_men_da_qiao", "ao_men,_da_dao"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strings);
autoCompleteTextView.setAdapter(arrayAdapter);
/**
* 1.初始化控件
* 2.创建一个适配器
* 3.绑定适配器
*/
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.auto_complete_textView);
String[] strings = {"beijing", "beijing321", "beijing_tian_an_men", "aomen", "ao_men_da_qiao", "ao_men,_da_dao"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strings);
autoCompleteTextView.setAdapter(arrayAdapter);
/**
* 1.初始化控件
* 2.创建一个适配器
* 3.绑定适配器
*/
MultiAutoCompleteTextView multiAutoCompleteTextView = findViewById(R.id.multi_auto_complete_textView);
multiAutoCompleteTextView.setAdapter(arrayAdapter);
// 设置一个分隔符 用以分割不同 词组
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
// 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="@+id/auto_complete_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
***android:completionThreshold="2"***
android:hint="@string/hint"
android:textColor="@android:color/holo_orange_light"
android:textSize="16sp"/>
<MultiAutoCompleteTextView
android:padding="10dp"
android:id="@+id/multi_auto_complete_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_recevier" />
</LinearLayout>
***android:completionThreshold="2"*** 这是表示输入几个文字显示自动匹配的内容