Android实现智能提示的文本输入框AutoCompleteTextView
今天我们要讲一个十分简单的内容,就是一个安卓控件的使用,用法很简单,但是很常用的一个。这里我用两种不同的写法来处理。当然,无论用哪一种写法,效果都是一样的。
我们先来看效果图。

要实现这种效果十分简单。需要一个控件,AutoCompleteTextView,他是EidtView的子类
我们先看第一种写法
先看布局文件
<?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="horizontal" > <AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
>
</AutoCompleteTextView> </LinearLayout>
autocomplete.xml
然后就主类文件
package com.example.mydemo.autotextview; import java.util.ArrayList;
import java.util.List; import com.example.mydemo.R; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; /**
***************************************************************
*
* @版权 LinFeng
*
* @作者 LinFeng
*
* @版本 1.0
*
* @创建日期 2016-6-14
*
* @功能描述 智能提示的文本框
*****************************************************************
*/
public class AutoCompleteActivtiy extends Activity { private AutoCompleteTextView aTextView;
private ArrayList<String> list;
private ArrayAdapter<String> adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);
aTextView = (AutoCompleteTextView) findViewById(R.id.auto);
/**
* 定义一个设配器
*/
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData());
aTextView.setAdapter(adapter); } /**
* 智能提示的文本内容存储在集合中,实际开发中,一般我们数据在服务器中获取
* 在输入过程中,我们要注意,必须输入两个及以上的字符才会有提示哦
* @return
*/
public List<String> getData(){
list = new ArrayList<String>();
list.add("JoyceChu0");
list.add("JoyceChu1");
list.add("JoyceChu2");
list.add("JoyceChu3");
list.add("JoyceChu4"); return list; } }
AutoCompleteActivtiy
这个就是第一种写法,十分简单
然后我们看看第二种写法,第二种写法其实和第一种很像,只是我们把数据写入资源文件的strings.xml中而已。所以我们需要先看看strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">MyDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<!-- 定义一个数组资源 -->
<string-array name="tpis">
<item>JoyceChu0</item>
<item>JoyceChu1</item>
<item>JoyceChu2</item>
<item>JoyceChu3</item>
<item>JoyceChu4</item>
</string-array> </resources>
strings.xml
然后主文件只需要做很小的修改就可以了,其实就是拿到数组数据,然后把数据写到设配器里面就可以了。
String srt[] = getResources().getStringArray(R.array.tpis);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, srt);
Android实现智能提示的文本输入框AutoCompleteTextView的更多相关文章
- Android 自学之自动完成文本框 AutoCompleteTextView
自动完成文本框(AutoCompleteTextView)从EditText派生而出,实际上他也是一个编辑框,但他比普通的编辑框多了一个功能:当用户输入一定字符后,自动完成文本框会显示一个下拉菜单,供 ...
- 【WPF】自动完成/智能提示的文本框(AutoCompleteBox)
使用了插件WPFToolKit.(直接在Nuget中搜即可) 使用方法参考这篇文章: http://www.broculos.net/2014/04/wpf-autocompletebox-autoc ...
- android studio 智能提示忽略大小写
Step1: Step2:
- Android文本输入框(EditText)切换密码的显示与隐藏
package cc.c; import android.app.Activity; import android.os.Bundle; import android.text.Selection; ...
- Android自己主动提示文本框(AutoCompleteTextView)
自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...
- Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView
AutoCompleteTextView: 就是一个带自动提示的EditText,当输入字符时,会出现提示. android:completionThreshold 输入几个字符时提示 androi ...
- Android自动提示控件:AutoCompleteTextView和MultiAutoCompleteTextView
在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. 一.AutoCompleteTextView:单一匹配 ...
- Android零基础入门第47节:自动完成文本框AutoCompleteTextView
原文:Android零基础入门第47节:自动完成文本框AutoCompleteTextView 上一期学习的Spinner的使用,掌握的怎么样?本期一起来学习AutoCompleteTextView的 ...
- Android开发10.2:UI组件AutoCompleteTextView(自动完成文本框)
概述 AutoCompleteTextVeiw(自动完成文本框)从 EditText派生而出 PS :EditText用法介绍 当用户输入一定字符后,自动完成自动完成文本框会显示 ...
随机推荐
- SqlCacheDependency轮询数据库表的更改情况的频率
下面的示例向 ASP.NET 应用程序添加一个 SqlCacheDependency. <sqlCacheDependency enabled="true" pollTi ...
- struts2学习(4)
Struts2拦截器概述 1 Struts2是框架,封装了很多功能,struts2里面封装的概念都是在拦截器里面 2 Struts2里面封装了很多的概念,有很多拦截器,不是每次这些拦截器都执行,每次执 ...
- nginx基本参数详解
运行用户 user nobody; 启动进程,通常设置成和cpu的数量相等 worker_processes 1; 全局错误日志及PID文件 error_log logs/error.log; err ...
- ural 2016 Magic and Science
2016. Magic and Science Time limit: 1.0 secondMemory limit: 64 MB Scientists who specialize in witch ...
- nyoj 42 一笔画 欧拉通路
http://acm.nyist.net/JudgeOnline/problem.php?pid=42 一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc ...
- AI探索(一)基础知识储备
AI的定义 凡是通过机器学习,实现机器替代人力的技术,就是AI.机器学习是什么呢?机器学习是由AI科学家研发的算法模型,通过数据灌输,学习数据中的规律并总结,即模型内自动生成能表达(输入.输出)数据之 ...
- mysql查询哪张表数据最大
转载:https://blog.csdn.net/qq13650793239/article/details/81142134 mysql数据库中information_schema 数据库存储了数据 ...
- Java 接口与继承 道至简第六章发表阅读笔记
一.继承条件下的构造方法调用 class Grandparent { public Grandparent() { System.out.println("GrandParent Creat ...
- LeetCode Shopping Offers
原题链接在这里:https://leetcode.com/problems/shopping-offers/description/ 题目: In LeetCode Store, there are ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...