当我们在做项目的时候,往往有着这样的需求:(1)在登陆的时候,在输入的账号的过程中弹出补全账号的提示(2)在搜索的时候,在输入搜索词的过程中弹出补全搜索词的提示,就像:
而这时,AutoCompleteTextView自动补全文本框则可以简易的帮我们解决这个难题:
实现代码:
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xiaoyan.xiejinxiong.testintent.MainActivity">
android:id="@+id/actv"
android:layout_width="200dp"
android:layout_height="wrap_content" />
MainActivity.javapublic class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.actv);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, new String[]{"String", "studio", "steam"});
autoCompleteTextView.setAdapter(arrayAdapter);
}
}效果便是上图了。
不过,这个提示的布局是系统默认布局,我们可以根据自己的需求去修改其布局,将
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, new String[]{"String", "studio", "steam"});修改为:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.text_list_item, R.id.tvlist,new String[]{"String", "studio", "steam"});text_list_item.xml
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
android:id="@+id/tvlist"
android:textColor="@android:color/holo_blue_bright"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
效果图: