自动完成文本框(AutoCompleteTextView与MultiAutoCompleteTextView)关联适配器
2024-10-21 16:01:48
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson6_5_id19.MainActivity" >
<!--单个字段扩充提示
completionThreshold是控制用户输入第几个字符开始提示,最小值是1-->
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="1.请输入联系人"
android:completionThreshold="1"
android:singleLine="true"/> <!-- 多个字段扩充提示 -->
<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2.请输入联系人"
android:completionThreshold="1"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 自定义提示筐的TextView -->
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示文本"
android:gravity="center"
android:padding="10dp"
android:background="#ccffcc"
android:textColor="#ff0000"/>
</LinearLayout>
package com.example.lesson6_5_id19; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView; public class MainActivity extends Activity {
// 把提示的内容放到一个String数组里面
String[] names = {"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",};
AutoCompleteTextView actv;
MultiAutoCompleteTextView mactv;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv);
// 适配器
// 将数据与视图关联在一起
// 系统已经帮我们写好了很多layout
// adapter = new ArrayAdapter<String>(this//1.上下文
// ,R.layout.item_actv_layout//2.布局
// ,R.id.tv//该布局上的TextView的id号
// ,names);//最后一个是数据
// 选择系统的layout,里面的TextView的id号可以忽略不写
adapter = new ArrayAdapter<String>(this, R.layout.activity_item, R.id.tv, names);
//使用“,”来分割。
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// 将适配器设置到view上
actv.setAdapter(adapter);
//设置适配器之前,必须要知道分隔符是什么样的。
mactv.setAdapter(adapter);
}
}
最新文章
- maven---install报错
- 完美C++(第5版)(双色)
- Python之SQLAlchemy学习--外键约束问题
- Sublime Text2 安装Package Control
- jquery 删除字符串最后一个字符的方法
- 帝国CMS商城功能高级使用
- java语言基础02
- 51nod贪心算法入门-----任务分配问题
- Lambda表达式转SQL语句类库
- loadrunner controller:集合点策略
- 使用Swashbuckle构建RESTful风格文档
- 利用kibana插件对Elasticsearch进行bool查询
- Scrapy实战篇(六)之爬取360图片数据和图片
- 初学c# -- 纯属无聊,写下棋
- [转]linux 调用动态库so文件
- Anaconda下载及安装及查看安装的Python库用法
- element ui里dialog关闭后清除验证条件
- [osg][osgEarth][原]基于OE自定义自由飞行漫游器(初级版)
- 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI
- day15(mysql之零碎知识)