Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
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控件实现类似百度搜索提示,限制输入数字长度的更多相关文章
- Android 控件 -------- AutoCompleteTextView 动态匹配内容,例如 百度搜索提示下拉列表功能
AutoCompleteTextView 支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据.两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开 ...
- Android:控件AutoCompleteTextView 自动提示
在文本框中输入,要这样的提示效果,如果你输入的是aac,在输入aa后,选择aac,文本框的内容会自动补齐,输入aac(类似百度搜索文本框的显示结果) <AutoCompleteTextVie ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android 轮换控件
首先是控件轮换 一.创建主布局 1.用到的控件是 TextSwitcher (文本轮换) 那么其他对应的也就是 ImageSwitcher (图片轮换) <LinearLayout xmlns: ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- Github上star数超1000的Android列表控件
Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...
- 【Android开发日记】之入门篇(十三)——Android的控件解析
Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...
- 一个Demo让你掌握Android所有控件
原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士" 下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...
随机推荐
- Struts2---环境搭建及包介绍
导入jar包 jar包下载地址:http://www.apache.org/官网中选择struts,然后点击download下载.将jar包导入到WEB-INF下的lib文件目录下. asm-5.2. ...
- erlang连接mysql [转]
转自: http://blog.csdn.net/flyinmind/article/details/7740540 项目中用到erlang,同时也用到mysql.惯例,google. 但是,按照网上 ...
- gulp批量添加版本号
要实现的理想效果: "/css/style.css" => "/dist/css/style.css?v=1d87bebe""/js/scrip ...
- 转载——一步步学习js
一步步学习javascript基础篇(0):开篇索引 阅读目录 索引: 一步步学习javascript基础篇(1):基本概念 一步步学习javascript基础篇(2):作用域和作用域链 一步步学习j ...
- 利用binlog server及Xtrabackup备份集来恢复误删表(drop)
Preface Today I'm gonna test how to rescue a dropped table from binlog server based on a ful ...
- selenium 使用谷歌浏览器模拟wap测试
/** * 使用谷歌浏览器模拟手机浏览器 * @param devicesName * @author xxx * 创建时间:2017-06-15,更新时间:2017-06-15 * 备注 */ pu ...
- Canvas 图片平铺设置
/** * 图片平铺 */ function initDemo7(){ var canvas = document.getElementById("demo7"); if (!ca ...
- excel批量导入
https://www.cnblogs.com/mingyue1818/p/4828865.html
- Hibernate 查询方法
1.简单查询: public User select(User user) { User newUser; try { newUser = (User) session.get(User.class, ...
- 安卓context(一)
最近看了一下深入安卓内核,毫无安卓基础的我一头深入不可自拔,看的是云里雾里,第一遍看到一半左右似乎开始失去了效率. 现在开始第二遍,并对看过的重要知识点以个人的理解进行梳理(不免有错的地方,好心人请告 ...