这里只谈简单的使用:
代码xml:
1 <AutoCompleteTextView 2 android:id="@+id/actv" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:completionThreshold="1" 6 />
1、android:completionThreshold="1"属性:
这个属性是设置用户输入多少个字符才会显示提示。
MainActivity:
1 public class MainActivity extends Activity { 2 3 //[0]声明AutoCompleteTextView要显示的数据 4 private static final String[] COUNTRIES = new String[] { 5 "laofang", "laozhang", "laoli", "laobi","laoli","laowang","aab","abb","cc" 6 }; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 //[1]找到控件 13 AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv); 14 15 //[2]创建数据适配器 16 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 17 android.R.layout.simple_dropdown_item_1line, COUNTRIES); 18 19 //[3]设置数据适配器 20 actv.setAdapter(adapter); 21 } 22 }