日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

發(fā)布時(shí)間:2023/12/13 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android AutoCompleteTextView 控件實(shí)現(xiàn)類似被搜索提示,效果如下

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位數(shù)字"android:inputType="number" /></LinearLayout></LinearLayout>

2.控件下拉列表項(xiàng)布局文件?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 代碼實(shí)現(xiàn):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, "你輸入的字?jǐn)?shù)已經(jīng)超過(guò)了限制!",Toast.LENGTH_SHORT).show();s.delete(editStart - 1, editEnd);int tempSelection = editStart;mEditText.setText(s);mEditText.setSelection(tempSelection);}}}; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/_ymw/p/4147212.html

總結(jié)

以上是生活随笔為你收集整理的Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。