【Android 开发教程】AutoCompleteTextView

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


AutoCompleteTextView和EditText很相似,事实上,AutoCompleteTextView就是EditText的子类。使用AutoCompleteTextView,当用户正在输入时,会自动弹出一些提示信息。下面的例子将会展示如何使用AutoCompleteTextView去自动地帮助用户完成输入。

1。 创建一个工程:BasicViews3。

2。 main.xml中的代码。

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="NameofPresident"/>
  10. <AutoCompleteTextViewandroid:id="@+id/txtCountries"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"/>
  13. </LinearLayout>
3、BasicViews3Activity.java中的代码。
[java] view plain copy
  1. publicclassBasicViews3ActivityextendsActivity{
  2. String[]presidents={
  3. "DwightD.Eisenhower",
  4. "JohnF.Kennedy",
  5. "LyndonB.Johnson",
  6. "RichardNixon",
  7. "GeraldFord",
  8. "JimmyCarter",
  9. "RonaldReagan",
  10. "GeorgeH.W.Bush",
  11. "BillClinton",
  12. "GeorgeW.Bush",
  13. "BarackObama"
  14. };
  15. /**Calledwhentheactivityisfirstcreated.*/
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. ArrayAdapter<String>adapter=newArrayAdapter<String>(this,
  21. android.R.layout.simple_dropdown_item_1line,presidents);
  22. AutoCompleteTextViewtextView=(AutoCompleteTextView)
  23. findViewById(R.id.txtCountries);
  24. textView.setThreshold(3);
  25. textView.setAdapter(adapter);
  26. }
  27. }
4、F11调试。 【Android 开发教程】AutoCompleteTextView

你可能感兴趣的:(【Android 开发教程】AutoCompleteTextView)