Android 程式开发:(十)基本控件 —— 10.4 AutoCompleteTextView

本文介绍了一个使用AutoCompleteTextView的示例应用,展示了如何为用户提供自动完成建议。通过创建一个简单的应用,用户可以在输入美国总统名字时获得自动补全选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1、创建一个工程:BasicViews3。


2、main.xml中的代码。

  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中的代码。
  1. packagenet.learn2develop.BasicViews3;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.ArrayAdapter;
  5. importandroid.widget.AutoCompleteTextView;
  6. publicclassBasicViews3ActivityextendsActivity{
  7. String[]presidents={
  8. "DwightD.Eisenhower",
  9. "JohnF.Kennedy",
  10. "LyndonB.Johnson",
  11. "RichardNixon",
  12. "GeraldFord",
  13. "JimmyCarter",
  14. "RonaldReagan",
  15. "GeorgeH.W.Bush",
  16. "BillClinton",
  17. "GeorgeW.Bush",
  18. "BarackObama"
  19. };
  20. /**Calledwhentheactivityisfirstcreated.*/
  21. @Override
  22. publicvoidonCreate(BundlesavedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. ArrayAdapter<String>adapter=newArrayAdapter<String>(this,
  26. android.R.layout.simple_dropdown_item_1line,presidents);
  27. AutoCompleteTextViewtextView=(AutoCompleteTextView)
  28. findViewById(R.id.txtCountries);
  29. textView.setThreshold(3);
  30. textView.setAdapter(adapter);
  31. }
  32. }
4、F11调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值