TextView的升级版———AutoCompleteTextView

AutoCompleteTextView顾名知义,可以自动提示的TextView,还可以提示错误信息。

这里介绍基本的使用,能够满足绝大部分需求

  • 效果图

XML布局

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity"> <AutoCompleteTextView
android:id="@+id/actv_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="error"
android:text="提示错误" />
</LinearLayout>

测试类

package com.example.kongqw.autocompletetextviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; public class MainActivity extends AppCompatActivity { private AutoCompleteTextView mAutoCompleteTextView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.actv_username);
String[] emailAddressCollection = {"kongqw@foxmail.com", "qq4878802", "123@qq.com", "123456789", "123888888"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, emailAddressCollection);
mAutoCompleteTextView.setAdapter(adapter);
} // 提示错误
public void error(View view){
mAutoCompleteTextView.setError("这里是错误提示");
}
}

自动提示

核心方法就是AutoCompleteTextView的setAdapter()

这里模拟了几个已经记住的字符串,实际应用当中,这些数据可以读数据或者怎样,根据实际情况

mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.actv_username);
String[] emailAddressCollection = {"kongqw@foxmail.com", "qq4878802", "123@qq.com", "123456789", "123888888"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, emailAddressCollection);
mAutoCompleteTextView.setAdapter(adapter);

显示错误信息

mAutoCompleteTextView.setError("这里是错误提示");

修改下划线颜色

修改res->values->styles.xml下”AppTheme”里的colorAccent属性

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
……
<item name="colorAccent">#00DD00</item>
</style>
  • 效果(#00DD00)

补充

AutoCompleteTextView可以和TextInputLayout结合使用,效果还是不错的

TextInputLayout文章:http://blog.csdn.net/q4878802/article/details/49780691

  • 效果图

AutoCompleteTextView还有好多方法,刚兴趣的自行研究下把

TextView的升级版———AutoCompleteTextView的更多相关文章

  1. android test控件

    1.Plain Text 输入文本框 <EditText android:id="@+id/editText" android:layout_width="wrap ...

  2. 再不迁移到Material Design Components 就out啦

    翻译自国外文档加自己理解 原文 我们最近宣布了 Material Design Components(MDC)1.1.0 ,这是一个库更新,为您的 Android 应用程序带来了 Material T ...

  3. Third Day:正式编程第三天,学习实践内容TextView跑马灯、AutoCompleteTextView、multiAutoCompleteTextView以及ToggleButton、checkedBox、RadioButton等相关实践

    2.针对Focused的TextView跑马灯(文字较多一行无法显示)效果 针对单个TextView的跑马灯效果,可直接在TextView控件参数中添加三个属性: android:singleLine ...

  4. android内部培训视频_第三节 常用控件(Button,TextView,EditText,AutocompleteTextView)

    第三节:常用控件 一.Button 需要掌握的属性: 1.可切换的背景 2.9.png使用 3.按钮点击事件 1)  onClick 3) 匿名类 4) 公共类 二.TextView 常用属性 1.a ...

  5. AutoCompleteTextView自动补全文本框

    AutoCompleteTextView的作用是在输入框中输入我们想要输入的信息,就会出现其他与其相关的提示信息 下面是实例代码: MainActivity.java package com.shao ...

  6. Android 自动补全提示输入AutoCompleteTextView、 MultiAutoCompleteTextView

    以在搜索框搜索时,自动补全为例: 其中还涉及到一个词,Tokenizer:分词器,分解器. 上效果图: MainActivity.java: package com.joan.testautocoml ...

  7. 用AutoCompleteTextView实现历史记录提示

    自定义AutoCompleteTextView 博客分类: android进阶 android  网上找到的都是同ArrayAdapter一起使用的,有时候需要自定义风格,咋办?follow me! ...

  8. Android--UI之AutoCompleteTextView

    前言 之前讲过EditText,有兴趣的朋友可以看一下.这篇博客主要说明的是自动完成文本框,它实际上也是一个文本编辑框,可以理解为对EditText功能的扩展,它对输入的内容可以进行提示并且自动完成. ...

  9. Android UI系列-----EditText和AutoCompleteTextView

    在这篇随笔里将主要讲解一下EditText和AutoCompleteTextView这个控件 1.EditText 首先我们先简单来说说EditText这个控件,这个就相当于我们平常web开发中的文本 ...

随机推荐

  1. [LeetCode] Tag Validator 标签验证器

    Given a string representing a code snippet, you need to implement a tag validator to parse the code ...

  2. [LeetCode] Number Complement 补数

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  3. Mac下安装oh-my-zsh

    Mac下自带的终端并不好用,当你打开终端的时候是一个白花花的窗口,其实Mac自带几种shell,默认使用的是bash,可以通过 cat /etc/shells 查看几种shell bin/bash / ...

  4. 使用C# (.NET Core) 实现组合设计模式 (Composite Pattern)

    本文的概念性内容来自深入浅出设计模式一书. 本文需结合上一篇文章(使用C# (.NET Core) 实现迭代器设计模式)一起看. 上一篇文章我们研究了多个菜单一起使用的问题. 需求变更 就当我们感觉我 ...

  5. [SDOI 2009]HH去散步

    Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但 是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又 ...

  6. 计蒜客NOIP模拟赛4 D1T3 小X的佛光

    小 X 是远近闻名的学佛,平日里最喜欢做的事就是蒸发学水. 小 X 所在的城市 X 城是一个含有 N 个节点的无向图,同时,由于 X 国是一个发展中国家,为了节约城市建设的经费,X 国首相在建造 X ...

  7. ●Joyoi Easy

    题链: http://www.joyoi.cn/problem/tyvj-1952题解: 概率dp (先做的BZOJ 4318: OSU![本人题解],然后就感觉这个题很简单了) 令p[i]表示第i个 ...

  8. bzoj 2594: [Wc2006]水管局长数据加强版

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  9. hdu 5476 (计算几何)

    题意:求三角形内∠MPB+∠APC=∠MPC+∠APB的轨迹长度- - 1.基于M的中垂线       2.三角形内的圆弧(比赛只有看自己能否猜中),ps.以下是别人家的证明 #include < ...

  10. Codeforces Round #430 D. Vitya and Strange Lesson

    Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of numbers is ...