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. Android stadio Switch repository Android stadio切换仓库

    Android stadio 有时候,有很多module. 这些module 都有自己的仓库.也就是不在一块.那么,Android stadio 默认管理的就是根git. 如图,画对号的就是默认的. ...

  2. android onCreate的两个方法

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { } override ...

  3. (C)spring boot读取自定义配置文件时乱码解决办法

    这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...

  4. 《Cracking the Coding Interview》——第3章:栈和队列——题目1

    2014-03-18 03:19 题目:用一个数组实现3个栈. 解法: 首先我想过让三个栈动态决定长度.要么左右各一个向中间靠拢,要么三个穿插着,后来都觉得实现起来太复杂,而且思路总有各种功能缺陷,会 ...

  5. linux统计分析流量-wireshark

    wireshark是一款带界面的开源抓包工具,可以用来对系统流量进行统计分析. 安装 由于wireshark是带界面的,所以一般在界面环境下运行,可以通过yum安装: $ yum install -y ...

  6. Python 实现MD5加密

    from hashlib import md5 def encrypt_md5(s): # 创建md5对象 new_md5 = md5() # 这里必须用encode()函数对字符串进行编码,不然会报 ...

  7. Lua1

    使用lua进行脚本编程有很多优点: 1 代码体积小 2 执行速度快 3 安全性较高等 4 但是最大的优点是修改后的代码不需要重新编译即可生效,而高级语言的代码经过修改后需要经过重新编译或者解释后才能生 ...

  8. sshd_config_for_centos

    # $OpenBSD: sshd_config,v // :: djm Exp $ # This is the sshd server system-wide configuration file. ...

  9. virsh command

    http://www.cnblogs.com/chenjiahe/category/845519.html resize qemu-img resize win2k8r2.qcow2 40G conv ...

  10. Python下安装protobuf

    1. 下载安装包 2. 解压缩 tar –xzvf protobuf-2.6.1.tar.gz 3. 安装protoc 在python中使用protocbuf需要Protocal Buffer 编译器 ...