Android--edit box change style

Source: Internet
Author: User

1.

2. Implementing the Code

Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= " Match_parent "    android:layout_height=" Match_parent ">    <EditText         android:id=" @+id/txtsearch "        android:layout_width=" Match_parent "        android:layout_height=" Wrap_ Content "        android:hint=" Please enter "/></linearlayout>

Mainactivity.java

Importandroid.app.Activity;Importandroid.content.res.Resources;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;Importandroid.text.Editable;ImportAndroid.text.InputType;Importandroid.text.TextUtils;ImportAndroid.text.TextWatcher;ImportAndroid.view.Menu;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateDrawable Miconsearchdefault;//Search text box default icon    PrivateDrawable Miconsearchclear;//Search text box Clear text content icon    PrivateEditText Msearchview =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get the icon file inside the resource        FinalResources res =getresources (); //the default iconMiconsearchdefault =res.getdrawable (R.drawable.txt_search_default); //Clear IconMiconsearchclear =res.getdrawable (r.drawable.txt_search_clear); Msearchview=(EditText) Findviewbyid (R.id.txtsearch);        Msearchview.addtextchangedlistener (tbxsearch_textchanged);    Msearchview.setontouchlistener (Txtsearch_ontouch); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    /*** Determine if there is data in the input box, and then display the corresponding icon file*/    PrivateTextwatcher tbxsearch_textchanged =NewTextwatcher () {//caches whether the last text box is empty        Private BooleanIsNull =true; @Override Public voidaftertextchanged (Editable s) {if(Textutils.isempty (s)) {if(!isnull) {Msearchview.setcompounddrawableswithintrinsicbounds (NULL,                            NULL, Miconsearchdefault,NULL); IsNull=true; }            } Else {                if(IsNull) {msearchview.setcompounddrawableswithintrinsicbounds (NULL,                            NULL, Miconsearchclear,NULL); IsNull=false; } }} @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) {        }        /*** Change list content dynamically as text box contents change*/@Override Public voidOnTextChanged (Charsequence S,intStartintbefore,intcount)        {        }    }; //handle event when clear icon is clicked    PrivateOntouchlistener Txtsearch_ontouch =NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_up:intCurX = (int) Event.getx (); if(CurX > V.getwidth ()-&&!Textutils.isempty (Msearchview.gettext ())) {Msearchview.settext (""); intCacheinputtype = Msearchview.getinputtype ();//Backup//The input//typeMsearchview.setinputtype (Inputtype.type_null);//Disable//Soft//inputMsearchview.ontouchevent (event);//Call Native HandlerMsearchview.setinputtype (Cacheinputtype);//Restore InputToast toast = Toast.maketext (mainactivity. This, "Hello.", Toast.length_short);                                                                                    Toast.show (); //type                    return true;//consume touch even                }                 Break; }            return false; }    };}

3. Pictures to use

4. Description

In the farmer Uncle's blog to see, because can not find the URL, hereby explain

Android--edit box change style

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.