Android Autocompletetextview control implementation similar to Baidu search hint, limit input number length

Source: Internet
Author: User

The Android Autocompletetextview control implements similar search hints, with the following effects

1. First post the layout code 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= "Only 10 digits can be entered"Android:inputtype= "Number"/> </LinearLayout></LinearLayout>

2. Control drop-down list item layout file 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 Code implementation: Mainactivity.java

 PackageCom.example.autocompletetextview;Importjava.util.ArrayList;ImportJava.util.HashMap;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.AutoCompleteTextView;ImportAndroid.widget.EditText;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {ArrayListNewArraylist();    Autocompletetextview Autocompletetextview; PrivateEditText Medittext; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Additems (); Autocompletetextview=(Autocompletetextview) Findviewbyid (R.ID.AUTOCOMPLETETV); Simpleadapter Notes=NewSimpleadapter ( This, List, R.layout.main_item_row,NewString[] {"Brandsearchtext",                        "Brandname"},New int[] {r.id.searchtext, r.id.brandname});        Autocompletetextview.setadapter (Notes); Autocompletetextview.setthreshold (1); Autocompletetextview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3) {TextView TV=(TextView) Arg1.findviewbyid (r.id.brandname); Autocompletetextview.settext (Tv.gettext (). toString ()+ " ");            Autocompletetextview.setselection ((Autocompletetextview. GetText (). toString ()). Length ());        }        }); /**Textwatcher Operation*/Medittext=(EditText) Findviewbyid (r.id.et);    Medittext.addtextchangedlistener (Mtextwatcher); }    Private voidAdditems () {HashMap<string, string>item; Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", "NOKIA Nuojiya Njy"); Item.put ("Brandname", "Nokia");        List.add (item); Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", "Svmsun SX sanxing"); Item.put ("Brandname", "Samsung");        List.add (item); Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", "Svmsun SX Sanzhi"); Item.put ("Brandname", "Three squirrels");        List.add (item); Item=NewHashmap<string, string>(); Item.put ("Brandsearchtext", "Motorola Moto Mtll Motuoluola Motoloar"); Item.put ("Brandname", "Motorola");    List.add (item); } textwatcher Mtextwatcher=NewTextwatcher () {PrivateCharsequence temp; Private intEditstart; Private intEditend; @Override Public voidBeforetextchanged (Charsequence S,intArg1,intArg2,intArg3) {Temp=s; } @Override Public voidOnTextChanged (Charsequence S,intArg1,intArg2,intArg3) {} @Override Public voidaftertextchanged (Editable s) {Editstart=Medittext.getselectionstart (); Editend=Medittext.getselectionend (); if(Temp.length () > 10) {Toast.maketext (mainactivity. This, "You have entered more words than the limit!" ", Toast.length_short). Show (); S.delete (Editstart-1, Editend); intTempselection =Editstart;                Medittext.settext (s);            Medittext.setselection (tempselection); }        }    };}

Android Autocompletetextview control is similar to Baidu search hint, limit the input number length

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.