AutoCompleteTextView控件的使用
1.public class MainActivity extends Activity { 2. private String[] names = {"laowang","laozhang","laoli","xiaowang","xiaozhang","xiaoli"}; 3. @Override 4. protected void onCreate(Bundle savedInstanceState) { 5. super.onCreate(savedInstanceState); 6. setContentView(R.layout.activity_main); 7. AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv_text); 8. //给自动补全的textview设置一个数据适配器 这个适配器用来提供显示下拉列表的内容 9. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item, names); 10. //设置适配器 11. actv.setAdapter(adapter); 12. }
布局文件中声明对应节点
1.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2. xmlns:tools="http://schemas.android.com/tools" 3. android:layout_width="match_parent" 4. android:layout_height="match_parent" 5. android:paddingBottom="@dimen/activity_vertical_margin" 6. android:paddingLeft="@dimen/activity_horizontal_margin" 7. android:paddingRight="@dimen/activity_horizontal_margin" 8. android:paddingTop="@dimen/activity_vertical_margin" 9. tools:context=".MainActivity" > 10. 11. <AutoCompleteTextView 12. android:id="@+id/actv_text" 13. android:layout_width="match_parent" 14. android:layout_height="wrap_content" 15. android:completionThreshold="1" 16. android:hint="请输入内容" /> 17. 18.</RelativeLayout>
completionThreshold 通过这个属性来指定 输入多少个字符会出现提示