Android 之 自动匹配字符AutoCompleteTextView
2024-10-13 04:14:36
AutoCompleteTextView是自动匹配字符,当我们输入一个单词或一段话的前几个字时,就会自动为你匹配后面的内容看效果图:
下面是代码:
MainActivit:
package com.example.autocompletetextview; import java.util.ArrayList;
import java.util.List; import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; public class MainActivity extends ActionBarActivity { AutoCompleteTextView autoCompleteTextView = null;
AutoCompleteTextView auto2 = null;
private static final String[] COUNTRIES = {"china","canada","Belgium", "France", "Italy", "Germany", "Spain"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto1);
auto2 = (AutoCompleteTextView) findViewById(R.id.auto2); /*静态方式,自动匹配的值已经提前设置好了
* 第一个参数是指当前上下文
* 第二个参数是显示的匹配布局,此处用的是Android已有的布局
* 第三个参数是存储自动匹配的值的数组
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);
autoCompleteTextView.setAdapter(adapter); /*
* 动态方式,自动匹配的值由list决定
*/
List<String> list = new ArrayList<String>();
list.add("测试aadd");
list.add("测试object");
list.add("测试home");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.simple_1, list);
auto2.setAdapter(adapter2);
}
}
main_activity:
<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" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="静态实现:"/>
<AutoCompleteTextView
android:id="@+id/auto1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="动态实现:"/>
<AutoCompleteTextView
android:id="@+id/auto2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
simple_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
xmlns:android="http://schemas.android.com/apk/res/android"/>
最新文章
- linux配置oracle11G监听及本地网络服务 及 数据库建库
- 关灯游戏源码(iOS)
- java本地方法如何调用其他程序函数,方法详解2
- InheritableThreadLocal原理
- eclipse svn 修改了类名之后提交
- Myeclipse中如何修改Tomcat的端口号
- Qt在windows与Mac OS中获取执行程序版本号
- 初识Identity
- Flask Markup 上下文,request
- 问题:AJAX的send参数里,空格以及它后面的数据在传递时消失(已解决)
- 一道很经典的 BFS 题
- 错误 java.lang.ClassCastException: com.xx cannot be cast to ResourceBundle
- 蓝桥杯 剪邮票(dfs枚举 + bfs)
- [hive] hive 内部表和外部表
- docker安装openwrt镜像(不完美案例)
- Abp项目构建、swagger及代码生成器
- ** exception error: no match of right hand side value
- [HAOI2018]奇怪的背包
- springboot学习笔记-2 一些常用的配置以及整合mybatis
- 关于int *a; int &;a;a; int &;a; *a; int * &;a
热门文章
- Java 时间日期系列目录
- GOCR.js – 使用 JS 识别出图片中的文本
- hash函数为什么要选择对素数求余?
- [git]安装git-pylint-commit-hook提高python项目中的代码质量
- 1 Servlet开篇准备
- 在Kibana上格式化字段,更好的在dashboard上展示
- IOS开发UI基础UIImagePickerController的属性
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
- .Net 自定义应用程序配置
- AEAI WM V1.0 工作管理系统开源发版