1.AutoCompleteTextView,顾名思义即自动补全文本;
public class MainActivity extends AppCompatActivity { PRivate AutoCompleteTextView autoCompleteTextView; private ArrayAdapter<String> adapter; private static final String[] countries ={"China","Russia","USA","India","USA1","USA2","Haha","Xixi","Lala","lala","xixi"};@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);autoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView);adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,countries);autoCompleteTextView.setAdapter(adapter);}}还有布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shen.fourth.MainActivity"> <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>线性布局里放置一个AutoCompleteTextView;这样就可以实现文本的自动补全。注意:AutoCompleteTextView的自动补全系统默认是当输入两个字符以上时才会自动显示提示补全文本!当然也可以自己修改completionThreshold属性,设置为自定义数值。2.MultiAutoCompleteTextView,比AutoCompleteTextView加了个Multi就不太一样了,当然功能也更强大,强大体现在在一次自动补全文本后可以继续自动补全后边的文本。代码:public class MainActivity extends AppCompatActivity { private MultiAutoCompleteTextView multiAutoCompleteTextView; private ArrayAdapter<String> adapter; private static final String[] countries ={"China","Russia","USA","India","USA1","USA2","Haha","Xixi","Lala","lala","xixi"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,countries);multiAutoCompleteTextView.setAdapter(adapter);//此处设置分隔符,这样当补全一次文本后,后边的文本也可以自动补全 multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());}}布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shen.fourth.MainActivity"> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1"/></LinearLayout>与之前相比,此时线性布局里放置的是MultiAutoCompleteTextView;这里注意到有一个属性completionThreshold,这个属性是设置当输入多少个字符时才会出现提示补全文本的。此处设置为1,数值可以自己定义。完毕~
新闻热点
疑难解答