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" ><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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/259642.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259642.shtml
英文地址,請注明出處:http://en.pswp.cn/news/259642.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Centos/RHEL上查看主板型號

老是搞忘記&#xff0c;專門做個記錄&#xff1a; [rootmedia ~]# dmidecode | grep "Product Name" Product Name: To be filled by O.E.M.Product Name: B75M-D3V 修改默認語言&#xff1a;[chenshouyongmedia ~]$ cat /etc/sysconfig/i18n LANG"en_US.UTF-8…

java即時聊天系統畢業_(完整版)基于Java即時聊天系統的設計與實現畢業論文設計...

目錄1 前言...................................................................................................................................1.1 課題選題背景...................................................................................................…

杭電 1284 錢幣兌換問題【完全背包求方案總數】

解題思路&#xff1a;因為對于完全背包的狀態轉移方程f[v]max(f[v],f[v-c[i]]w[i])已經記錄了所有背包組成的方案&#xff0c;只不過通常問的是求最大值&#xff0c;現在要求方案總數 即為 f[v]sum(f[v],f[v-c[i]w[i]]), Problem Description在一個國家僅有1分&#xff0c;2分&…

java與算法_Java與算法之(1) - 冒泡排序

冒泡排序法的原理是&#xff0c;每次比較相鄰的兩個元素&#xff0c;如果它們的順序錯誤就把它們交換過來。例如對4 3 6 2 7 1 5這7個數字進行從小到大的排序&#xff0c;從最左側開始&#xff0c;首先比較4和3因為是從小到大排序&#xff0c;4和3的順序顯然是錯誤的&#xff0…

Js+XML 操作

我的xml文件Login.xml如下. <?xml version"1.0" encoding"utf-8" ?><Login><Character><C Text"熱血"Value"0"></C><C Text"弱氣"Value"1"></C><C Text"激情…

Java(Android)線程池

1、new Thread的弊端執行一個異步任務你還只是如下new Thread嗎&#xff1f; [java] view plaincopy new Thread(new Runnable() { Override public void run() { // TODO Auto-generated method stub } }).start(); 那你就out太多了&#xff0c;n…

JQuery鏈式操作簡單的菜單列表

看到這個簡單的菜單demo&#xff0c;也是為了再看看JQuery對DOM的操作&#xff0c;一直都記不牢&#xff0c;特別是siblings&#xff08;&#xff09;這個總是想不起來。 這次再過一遍JQuery&#xff0c;不管簡單的還是復雜的demo 還是堅持練習一遍吧&#xff01;只為記錄&…

java 網絡編程實驗_Java網絡編程入門實驗一涉及點

1.http://www.cr173.com/html/20128_all.html 【wireshark怎么抓包、wireshark抓包詳細圖文教程】2.http://blog.csdn.net/huangjin0507/article/details/51678858 【HTTP協議1&#xff1a;工作原理】3.https://www.cnblogs.com/1666818961-lxj/p/7210021.html 【網絡常用端口號…

node.js async流程控制器--queue(隊列)

queue流程控制器是一個并行的流程控制器,但是它與parallel的區別在于queue可以控制一次執行幾個函數,而parallel只是讓所有函數并行執行. 例子如下: var q async.queue(function (obj,cb) {setTimeout(function () {console.log(obj);cb(); },obj.time) },1)for (var i 0; i&…

利用JS實現點擊上一周或下一周卻換

1.頁面加載顯示當前年份的第幾周 效果如圖&#xff1a; html代碼&#xff1a; <font size"2" color"black"> <input id"btnweek5" type"button" class"btn" value"上周" οnclick"EduCommissio…

centos7網卡編輯_CentOS7修改網卡為eth0

1.編輯網卡信息[rootlinux-node2~]#cd /etc/sysconfig/network-scripts/ #進入網卡目錄[rootlinux-node2network-scripts]# mv ifcfg-eno16777728 ifcfg-eth0 #重命名網卡名稱[rootlinux-node2 network-scripts]#cat ifcfg-eth0 #編輯網卡信息TYPEEthernetBOOTPROTOstaticDEFR…

C# 微支付退款申請接口 V3.3.6

/// <summary>/// 微支付退款申請/// </summary>/// <param name"context"></param>/// <param name"returnMsg"></param>/// <returns></returns>public bool Refund(HttpContext context, ref string r…

[轉] 英語、計算機、互聯網與全球化

http://davidzhao.blog.51cto.com/4548102/1225732 轉載于:https://www.cnblogs.com/wowk/p/3169638.html

APNIC IP 庫

http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest轉載于:https://www.cnblogs.com/dlwj/p/6388162.html

java reference 傳引用_Java的引用(reference)---Roni

摘自《Java面向對象編程》一書,作者:孫衛琴 來源:www.javathinker.org在JDK1.2以前的版本中&#xff0c;當一個對象不被任何變量引用&#xff0c;那么程序就無法再使用這個對象。也就是說&#xff0c;只有對象處于可觸及狀態&#xff0c;程序才能使用它。這就像在日常生活中&am…

C# 以管理員身份運行程序

剛看了一篇博友寫的“以管理員身份運行程序”, 所以我也來寫一個簡單易懂的&#xff0c;簡單兩步搞定&#xff0c;不用寫任何代碼&#xff1a; 第一步&#xff1a; 右鍵選擇項目 > 添加 > 新建項 &#xff1b; 找到 應用程序清單文件&#xff0c;后綴名為manifest&#x…

會計轉行從事IT,如何在一年時間內全職學習?

2019獨角獸企業重金招聘Python工程師標準>>> https://www.zhihu.com/question/21427478/answer/18227060 轉載于:https://my.oschina.net/soho00147/blog/836138

VS2010中使用CL快速 生成DLL的方法

方案一&#xff1a; 1、命令行中輸入cl example.cpp&#xff0c;生成example.obj和example.lib文件。有可能還會提示“沒有入口點”的錯誤。這是因為我們的CPP中是要生成dll文件的&#xff0c;并沒有main()這樣的主函數作為入口點。如果是C文件&#xff0c;則輸入cl /c exampl…

java field 獲得值_反射通用獲取字段值

像之前回答的那樣&#xff0c;您應該使用&#xff1a;Object value field.get(objectInstance);有時更喜歡的另一種方法是動態調用getter。示例代碼&#xff1a;public static Object runGetter(Field field, BaseValidationObject o){// MZ: Find the correct methodfor (Met…

android 中如何模擬back鍵

主要是在使用Fragment時能夠返回前一級&#xff0c;所以才找到了這些資料。 有兩種方式可以實現&#xff0c;直接上代碼 方法1&#xff1a; public void onBack(){new Thread(){public void run() {try{Instrumentation inst new Instrumentation();inst.sendKeyDownUpSync(Ke…