Android搜索-AutoCompleteTextView的用法

这次介绍AutoCompleteTextView的两种用法

第一种:使用字符串数组为内容来源

[html]  view plain copy
  1. <AutoCompleteTextView  
  2.         android:id="@+id/auto"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.       
  6.     />  

这是 AutoCompleteTextView的布局文件。

[java]  view plain copy
  1. AutoCompleteTextView tv=null;  
  2.  tv=(AutoCompleteTextView) findViewById(R.id.auto);  
[java]  view plain copy
  1. String []str=new String[]{"Chine","Japan","Korean","Russian","USA","Hong Kong"};  

下面是设置ArrayAdapter

[java]  view plain copy
  1. ArrayAdapter aaa=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, str);  
  2. v.setAdapter(aaa);  
这样就完成了AutoCompleteTextView。

效果图

第二种方法:使用XML为数据来源

下面是xml文件,在values/strings.xml中

[java]  view plain copy
  1. <string-array name="str">  
  2.        <item>China</item>  
  3.        <item>Korean</item>  
  4.        <item>Japan</item>  
  5.     </string-array>  
下面设置ArrayAdapter
[java]  view plain copy
  1. ArrayAdapter aaa=ArrayAdapter.createFromResource(this, R.array.str, android.R.layout.simple_spinner_item);  
  2.         aaa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  3.         tv.setAdapter(aaa);  
这样就完成了




从效果图来看,有些不一样,我们可以根据需要来选择。


---------------------------------未完待续----------------------------------------


最总目标是实现类似iphone电话本的效果...



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值