1、AutoCompleteTextView

  自动完成功能,在文本框中输入字符,会出现匹配的自动提示。类似百度搜索。

XML代码

<?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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="城市:" /> <AutoCompleteTextView
android:id="@+id/actv1"
android:completionThreshold="3" //输入多少个字符会出现提示
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入城市" >
</AutoCompleteTextView> </LinearLayout >

Java代码

public class AutoCompleteTextViewDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_demo); //1、获取页面上的自动完成控件
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv1);
//2、创建一个数组,保存的数据,字符串数组或者List<String>
String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
//3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
//4、将适配器绑定到AutoCompleteTextView中
actv.setAdapter(citydatapter);
}
}

2、MultiAutoCompleteTextView

  支持多选的自动完成,可以实现类似邮箱收件人的多选效果。

XML代码

<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市:"
android:textSize="20sp" /> <MultiAutoCompleteTextView
android:id="@+id/mactv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="2"
android:ems="10"
android:hint="请输入城市" />
</LinearLayout> </LinearLayout>

Java代码:

public class AutoCompleteTextViewDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_demo); //1、获取页面上的自动完成控件
MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv1);
//2、创建一个数组,保存的数据,字符串数组或者List<String>
String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
//3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
//4、将适配器绑定到AutoCompleteTextView中
mactv.setAdapter(citydatapter);
//5、设置分隔符,以,进行分割
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}

Android学习(三) 自动完成的使用的更多相关文章

  1. android学习三---创建第一个程序

    1.创建一个Helloworld程序 1.1 new-android application 点击file-new-android application出现如下界面 填上应用名,项目名,包名,选择所 ...

  2. openfire Android学习(三)----会议室创建、加入以及查询会议室中所有成员等

    openfire 中的会议室不像QQ群一样,不能保存那些离线用户,加入会议室后,一旦断开连接,就会离开会议室. 虽然如此,但如果要实现也不是不可能,我们可以自己做后台来保存,有兴趣的可以去试着实现一下 ...

  3. Android学习三:线程操作

    作为Android开发的组成部分,多线程的作用举足轻重,先来说说应用场景 1多线程使用场景 1.1正常使用中,经常有子线程来更新界面UI的需求,但是安卓不允许子线程更新UI 使用子线程处理UI,若线程 ...

  4. Hibernate学习(三)自动建表

    一般情况下有如下两种方法: 1.在配置文件中添加如下配置 <property name="hibernate.hbm2ddl.auto">create</prop ...

  5. 【Android】完善Android学习(三:API 3.0)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  6. 【转】Pro Android学习笔记(三十):Menu(1):了解Menu

    目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...

  7. 【转】Pro Android学习笔记(三):了解Android资源(上)

    在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...

  8. 【Android】Eclipse自动编译NDK/JNI的三种方法

    [Android]Eclipse自动编译NDK/JNI的三种方法 SkySeraph Sep. 18th  2014 Email:skyseraph00@163.com 更多精彩请直接访问SkySer ...

  9. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

  10. Android JNI学习(三)——Java与Native相互调用

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

随机推荐

  1. 汕头市队赛 SRM1X T1

    木之本樱 背景 “西瓜是可以种在树上的!”——木之本樱 描述 空地上,一排排的西瓜树拔地而起. 魔法世界里,空地是无限大的.所有的树排成了n条直线,每条直线也是向左右两端无限延伸的. 由于自己姓木(之 ...

  2. aiohttp的学习

    https://hubertroy.gitbooks.io/aiohttp-chinese-documentation/content/aiohttp%E6%96%87%E6%A1%A3/Client ...

  3. resin启动问题

    启动resin时报错如下: Resin/4.0.28 can't restart -server 'app-0'. com.caucho.bam.RemoteConnectionFailedExcep ...

  4. 第二部分:Spring中配置mongodb

    一.需要引用的jar包 1.spring-data-mongodb-1.9.4.RELEASE.jar 2.spring-data-commons-1.12.11.RELEASE.jar 3.mong ...

  5. 删除 Myeclipse 遗留的 workspace

    有时因需要而创建多个 workspace 并在它们之间切换,但是如果某些 workspace 内容被物理删除(不再需要)后,当你点击 File --> Switch Workspace 时,旧的 ...

  6. Vue.js 2使用中的难点举例--子组件,slot, 动态组件,事件监听

    一例打尽..:) <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  7. OpenJDK中java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException解决办法

    http://www.cnblogs.com/xusweeter/p/9667801.html

  8. Ubuntu16.10 +python3.5+Tensorflow 1.1

    1.python版本检查 因为Ubuntu16.10已经默认安装了python2.7 和 3.5,检查python版本, 如果为python2.7,那么就需要我们设置python3.5为默认版本. 查 ...

  9. JQuery里面的知识

    JQuery是一个javaScript库 JQuery极大的简化了javaScript编程 通过点击 "TIY" 按钮来看看它是如何运行的. 演示JQuery的hide函数,隐藏了 ...

  10. Kth Smallest Element in a Sorted Matrix -- LeetCode

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...