1.XML布局
android:completionThreshold="1":这里我们设置了输入一个字就显示提示
(1)主界面布局
<?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:layout_height="match_parent" android:gravity="center" tools:context=".MainActivity"> <AutoCompleteTextView android:id="@+id/actv_show" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:completionThreshold="1" android:textColor="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
(2)Arrayadapter布局
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/dropDownItemStyle" android:textAppearance="?android:attr/textAppearanceLargePopupMenu" android:singleLine="true" android:layout_width="match_parent" android:textColor="#000000" android:layout_height="?android:attr/listPreferredItemHeight" android:ellipsize="marquee" />
2.java后台
package com.example.administrator.test59autocompletetextview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends AppCompatActivity { private static final String[] data = new String[]{ "小猪猪", "小狗狗", "小鸡鸡", "小猫猫", "小咪咪" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到AutoCompleteTextView控件 AutoCompleteTextView actv_show=findViewById(R.id.actv_show); //采用数组适配器,第一个参数是上下文,就是当前的Activity, 第二个参数是自己定义的一个布局,它里面只有一个TextView,第三个参数就是我们要显示的数据。 ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), R.layout.actv_layout, data); actv_show.setAdapter(adapter2); } }
3.效果图