android文本框自动补全,[Android]AutoCompleteTextView自动补全文本框

当我们在做项目的时候,往往有着这样的需求:(1)在登陆的时候,在输入的账号的过程中弹出补全账号的提示(2)在搜索的时候,在输入搜索词的过程中弹出补全搜索词的提示,就像:

adab5986e0175928c0740bbd82ede0cf.png

而这时,AutoCompleteTextView自动补全文本框则可以简易的帮我们解决这个难题:

41ad1bf0fbdb636323563c71319866da.png

实现代码:

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.xiaoyan.xiejinxiong.testintent.MainActivity">

android:id="@+id/actv"

android:layout_width="200dp"

android:layout_height="wrap_content" />

MainActivity.java

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.actv);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, new String[]{"String", "studio", "steam"});

autoCompleteTextView.setAdapter(arrayAdapter);

}

}效果便是上图了。

不过,这个提示的布局是系统默认布局,我们可以根据自己的需求去修改其布局,将

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, new String[]{"String", "studio", "steam"});修改为:

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.text_list_item, R.id.tvlist,new String[]{"String", "studio", "steam"});text_list_item.xml

android:layout_width="match_parent"

android:padding="10dp"

android:layout_height="match_parent">

android:id="@+id/tvlist"

android:textColor="@android:color/holo_blue_bright"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

效果图:

16bdb91556cbcd97f57803c9c51da298.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值