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" ><AutoCompleteTextViewandroid:id="@+id/autocomplete"android:layout_width="fill_parent"android:layout_height="wrap_content" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:orientation="horizontal" ><TextViewandroid: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" /><EditTextandroid: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" ><TextViewandroid:id="@+id/brandName"android:layout_width="fill_parent"android:layout_height="30dp"android:textSize="18sp" /><TextViewandroid: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;@Overridepublic 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() {@Overridepublic 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;@Overridepublic void beforeTextChanged(CharSequence s, int arg1, int arg2,int arg3) {temp = s;}@Overridepublic void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {}@Overridepublic 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);}}};
}

转载于:https://www.cnblogs.com/_ymw/p/4147212.html

Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度相关推荐

  1. Android原生控件SearchView实现历史搜索记录

    SearchView实现搜索记录看了一些大神写的贴子简单实现, 但我们功能 需要单独删除一条历史搜索记录,好像没在网上找到解决方案,原生方法上也只有suggestions.clearHistory() ...

  2. Android Studio开发基础之AutoCompleteTextView控件的使用

    在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的.AutoCompleteTextView控件继承自Text ...

  3. android 实现控件搜索折叠效果 Animation动画折叠和普通折叠

    android 实现控件搜索折叠效果 Animation动画折叠和普通控件直接折叠效果: 原理很简单,都是获取对应的高度,通过实现高度变化而改变显示效果: 话不多说直接上代码 1.普通折叠效果,以下是 ...

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

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

  5. Android学习--02(猜猜我的星座App源码+Android常用控件TextView+EditText+Button+ImangeView+DatePicker+App间通信+跳转页面)

    猜猜我的星座App 1 Android常用控件 1.1 TextView控件 1.1.1 简介 1.1.2属性 1.1.3 扩展属性 1.1.4 TextView的使用方法 1.1.5总结 1.2 E ...

  6. Android开源控件收集整理

    一 .基本控件 TextView HTextView 一款支持TextView文字动画效果的Android组件库.GitHub - hanks-zyh/HTextView: Animation eff ...

  7. Android 原生控件之一 TextView

    Android 原生控件之一 TextView 前言 来源 开始 XML属性 1.android:allowUndo 2.android:autoLink 3.android:autoSizeMaxT ...

  8. Android图表控件MPAndroidChart——曲线图LineChart的使用(财富收益图)

    目录 前言 本文涉及文章 其他相关文章 1.数据准备 1.1 数据来源 2.曲线展示 2.1 MPAndroidChart获取 2.2 数据对象获取 2.3 数据展示 3.曲线完善 3.1 图表背景. ...

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

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

最新文章

  1. 客户端渲染 服务端渲染_这就是赢得客户端渲染的原因
  2. 华为全新发布开源操作系统欧拉:覆盖数字基础设施全场景
  3. 【BZOJ-2669】局部极小值 状压DP + 容斥原理
  4. Oracle 数据库sql语句查看字符集,PG数据库查询字符集方法
  5. 手机连接服务器传输文件,手机云服务器传输文件
  6. 模板 字段_Anki学习之路(08)|什么是Anki模板类型?什么是字段?
  7. Flink 在又拍云日志批处理中的实践
  8. 如何找到SAP ECC事务码升级到S4HANA后对应的新事务码
  9. 对new int[]()的理解(转载)
  10. shadow阴影属性
  11. tracepro应用实例详解_十大行业气动设备 120个典型气动系统应用实例
  12. ElementUI table表格数据html格式解析
  13. UVALive - 2911 Maximum
  14. m3u8下载时出现跨域的解决方法
  15. 快速通过论文相似度检测
  16. Jenkins 添加配置Git账号密码凭据
  17. 熵、图像熵的意义及计算方法
  18. JAVA视频学习笔记-马士兵(七)
  19. 设计一个彩灯循环控制器
  20. NC65【单据转换规则】新增下游单据类型,下游单据生成仅支持后台(以【到货单 23】to【其他入库单 4A】为例)

热门文章

  1. 软件开发过程中的思维方式 -- 如何分析问题
  2. MATLAB求音频信号特征的自定义函数.md
  3. 出国?上研?工作?回家种田?(三) IT类专业的工作方向与特点(软硬件技术等岗位)
  4. vue根据url获取内容axios_vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据...
  5. js粘贴板为什么获取不到图片信息_图床+typora,告别markdown中关于图片的困惑
  6. 鸿蒙首饰用什么合成,天下HD鸿蒙炼炉是什么?鸿蒙炼炉讲解
  7. asp 退出登录修改cookie能进入后台_深入浅出让你理解跨域与SSO单点登录原理与技术...
  8. java与bartender_Java调取Bartender使用教程.md
  9. python 常见问题汇总(待续)
  10. 质量属性六个常见属性场景(《淘宝网》为例) 15