基于Android中的 AutoCompleteTextView实现自动填充
时间:2023-04-02 07:11:36
现在我们上网会用百度或者谷歌搜索信息,当我们在输入框里输入一两个字后,就会自动提示我们想要的信息,这种效果在Android 是通过Android 的AutoCompleteTextView Widget 搭配ArrayAdapter 设计同类似Google 搜索提示的效果.
先在Layout 当中布局一个AutoCompleteTextView Widget ,然后通过预先设置好的字符串数组,将此字符串数组放入ArrayAdapter ,最后利用AutoCompleteTextView.setAdapter 方法,就可以让AutoCompleteTextView 具有自动提示的功能.例如,只要输入ab ,就会自动带出包含ab 的所有字符串列表.
public class MainActivity extends Activity {
private AutoCompleteTextView actv;
private static final String[] autoStrs = new String[] { "a", "abc", "abcde" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, autoStrs);
actv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/actv"
android:completionThreshold="1" /><!-- 设置只需要输入一个字就开始匹配 -->
</LinearLayout>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android垃圾回收机制解决内存泄露问题
![](https://img.aspxhome.com/file/2023/2/97832_0s.png)
Java事件机制要素及实例详解
C#访问SqlServer设置链接超时的方法
java编程实现根据EXCEL列名求其索引的方法
Springboot整合Freemarker的实现详细过程
![](https://img.aspxhome.com/file/2023/4/84164_0s.jpg)
Spring集成MyBatis 及Aop分页的实现代码
![](https://img.aspxhome.com/file/2023/5/64785_0s.png)
Java servlet、filter、listener、interceptor之间的区别和联系
Java开发学习 Eclipse项目有红感叹号解决之道
MyBatis动态Sql之if标签的用法详解
javafx实现时钟效果
![](https://img.aspxhome.com/file/2023/6/88196_0s.jpg)
springboot配置文件绑定实现解析
![](https://img.aspxhome.com/file/2023/7/61917_0s.png)
在Java中int和byte[]的相互转换
C#飞行棋小程序设计分析
![](https://img.aspxhome.com/file/2023/6/96396_0s.png)
C#使用log4net记录日志的方法步骤
![](https://img.aspxhome.com/file/2023/9/106449_0s.png)
java控制台输出百分比进度条示例
Java线程池源码的深度解析
![](https://img.aspxhome.com/file/2023/6/66636_0s.jpg)
C语言中的回调函数实例
idea 有时提示找不到类或者符号的解决
![](https://img.aspxhome.com/file/2023/9/60779_0s.jpg)
C#实现Excel转PDF时设置内容适应页面宽度
![](https://img.aspxhome.com/file/2023/6/116556_0s.png)