Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下

1.首先贴出布局代码 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="horizontal" > <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="Please input the text:"
android:textSize="18sp" /> <EditText
android:id="@+id/ET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="只能输入10位数字"
android:inputType="number" />
</LinearLayout> </LinearLayout>

2.控件下拉列表项布局文件 main_item_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/brandName"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="18sp" /> <TextView
android:id="@+id/searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" /> </LinearLayout>

3.java 代码实现:MainActivity.java

package com.example.autocompletetextview;

import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
AutoCompleteTextView autoCompleteTextView; private EditText mEditText; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItems();
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteTV);
SimpleAdapter notes = new SimpleAdapter(this, list,
R.layout.main_item_row, new String[] { "brandSearchText",
"brandName" }, new int[] { R.id.searchText,
R.id.brandName });
autoCompleteTextView.setAdapter(notes);
autoCompleteTextView.setThreshold(1); autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) arg1.findViewById(R.id.brandName);
autoCompleteTextView.setText(tv.getText().toString() + " ");
autoCompleteTextView.setSelection((autoCompleteTextView
.getText().toString()).length());
} }); /** TextWatcher操作 */
mEditText = (EditText) findViewById(R.id.ET);
mEditText.addTextChangedListener(mTextWatcher);
} private void addItems() {
HashMap<String, String> item; item = new HashMap<String, String>();
item.put("brandSearchText", "NOKIA nuojiya NJY");
item.put("brandName", "诺基亚");
list.add(item); item = new HashMap<String, String>();
item.put("brandSearchText", "SVMSUN SX sanxing");
item.put("brandName", "三星");
list.add(item); item = new HashMap<String, String>();
item.put("brandSearchText", "SVMSUN SX sanzhi");
item.put("brandName", "三只松鼠");
list.add(item); item = new HashMap<String, String>();
item.put("brandSearchText", "摩托罗拉 moto MTLL motuoluola motoloar");
item.put("brandName", "摩托罗拉");
list.add(item); } TextWatcher mTextWatcher = new TextWatcher() {
private CharSequence temp;
private int editStart;
private int editEnd; @Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
} @Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
} @Override
public void afterTextChanged(Editable s) {
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (temp.length() > 10) {
Toast.makeText(MainActivity.this, "你输入的字数已经超过了限制!",
Toast.LENGTH_SHORT).show();
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};
}

Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度的更多相关文章

  1. Android 控件 -------- AutoCompleteTextView 动态匹配内容,例如 百度搜索提示下拉列表功能

    AutoCompleteTextView 支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据.两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开 ...

  2. Android:控件AutoCompleteTextView 自动提示

    在文本框中输入,要这样的提示效果,如果你输入的是aac,在输入aa后,选择aac,文本框的内容会自动补齐,输入aac(类似百度搜索文本框的显示结果)   <AutoCompleteTextVie ...

  3. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  4. Android 轮换控件

    首先是控件轮换 一.创建主布局 1.用到的控件是 TextSwitcher (文本轮换) 那么其他对应的也就是 ImageSwitcher (图片轮换) <LinearLayout xmlns: ...

  5. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  6. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  7. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

  8. 【Android开发日记】之入门篇(十三)——Android的控件解析

    Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...

  9. 一个Demo让你掌握Android所有控件

    原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士"      下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...

随机推荐

  1. 从键盘输入数,输出它们的平方值&判断是不是2的阶次方数

    1.从键盘输入两个整数,然后输出它们的平方值和立方值 在Java中,没有像C语言那样有一个专供接受键盘输入值的scanf函数,所以一般的做法是从键盘输入一行字符,保存到字符串s中,再将字符组成的字符串 ...

  2. 《Cracking the Coding Interview》——第11章:排序和搜索——题目5

    2014-03-21 21:37 题目:给定一个字符串数组,但是其中夹杂了很多空串“”,不如{“Hello”, “”, “World”, “”, “”, “”, “Zoo”, “”}请设计一个算法在其 ...

  3. (整)Unreal渲染模块 框总览

    @author: 黑袍小道 随缘查看     说明 由于搬山的渲染这部分担心自己理解错误,故而搬移官方下,后面整个完成再反过来更新 (这当且仅当做Unreal的帮助文档).     图形编程 模块 渲 ...

  4. [译]在Linux中清空或删除大文件内容的5种方法

    原文来源: https://www.tecmint.com/empty-delete-file-content-linux/ 有时,在处理Linux终端中的文件时,您可能希望清除文件的内容,而无需使用 ...

  5. cfq调度器

    cfq调度是block层最复杂的一个调度器,主要思想是是说每个进程平均享用IO带宽,实现方法是在时间上对进程进行划分,以此达到平均占用IO的目的.带着几个问题去看cfq 1)现在进程来了之后,是插入到 ...

  6. tomcat调优(Mark)

    http://blog.csdn.net/jinwanmeng/article/details/7756591

  7. Python中的返回函数与闭包

    返回函数,顾名思义,就是高阶函数可以把函数作为return值返回.与闭包的关系是:闭包需要以返回函数的形式实现. 一. 返回函数 比如我们有一个求和函数: >>> def calc_ ...

  8. Android 热更新是如何实现的?

    Android开发中,我们常常遇到热更新这个概念,而这个热更新具体是怎么实现的呢?今天在网上看到一个大神分享的热更新相关实现原理和实现代码,感觉灰常不错,分享给广大码农盆友look look . Cl ...

  9. Asymptotic I Catalan Number

    卡特兰数出现在许多计数问题中. 常见的例子有:$n$ 个节点的有序二叉树,$2n$ 个括号构成的合法括号序列. 在上面所举的两个例子中,很容易看出卡特兰数满足递推: $$ C_{n+1} = \sum ...

  10. [bzoj] 1085 骑士精神 || ID-DFS

    原题 找到最少的步数成为目标状态. IDDFS(限制层数的dfs)即可 #include<cstdio> #include<algorithm> using namespace ...