Android Custom EditText input box with empty button

Source: Internet
Author: User

Summary Android custom edittext input box with empty button

When the user enters the word specifier edittext automatically appears to the right of the inside of the input box Delete button

Rewrite EditText to achieve simplified layout effect

:

Inherit EditText

Package Com.example.myedittexttest;import Android.content.context;import Android.graphics.rect;import Android.graphics.drawable.drawable;import Android.text.editable;import Android.text.textwatcher;import Android.util.attributeset;import Android.view.motionevent;import Android.widget.edittext;public class MyEditText Extends EditText {private final String TAG = "Myedittext";p rivate drawable dright;private Rect rbounds;public myedittext (C Ontext Paramcontext) {super (Paramcontext); Initedittext ();} Public Myedittext (Context paramcontext, AttributeSet paramattributeset) {super (Paramcontext, Paramattributeset); Initedittext ();} Public Myedittext (Context paramcontext, attributeset paramattributeset, int paramint) {super (Paramcontext, Paramattributeset, Paramint); Initedittext ();} Initializes the EditText control private void Initedittext () {setedittextdrawable (); Addtextchangedlistener (new Textwatcher () {// Listen for changes to text content @overridepublic void aftertextchanged (Editable parameditable) {} @Overridepublic void BeforetextchAnged (charsequence paramcharsequence, int paramInt1, int paramInt2, int paramInt3) {} @Overridepublic void ontextchanged ( Charsequence paramcharsequence, int paramInt1, int paramInt2, int paramInt3) {MyEditText.this.setEditTextDrawable ();}});} Controls the display of the picture public void setedittextdrawable () {if (GetText (). toString (). Length () = = 0) {setcompounddrawables (null, NULL, NULL, NULL);} else {setcompounddrawables (null, NULL, this.dright, NULL);}} @Overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); this.dright = Null;this.rbounds = null;}  /** * Add touch Event Click to empty edittext effect */@Overridepublic Boolean ontouchevent (Motionevent parammotionevent) {if (this.dright ! = null) && (parammotionevent.getaction () = = 1)) {this.rbounds = This.dRight.getBounds (); int i = (int) parammotio NEVENT.GETRAWX ()///distance from the screen//int i = (int) parammotionevent.getx ();//distance from the border if (i > GetRight ()-3 * This.rBounds.widt H ()) {SetText ("");p arammotionevent.setaction (Motionevent.action_cancel);}} return SUPEr.ontouchevent (parammotionevent);} /** * Show Right x picture's * * * left upper right lower * * @Overridepublic void Setcompounddrawables (drawable paramDrawable1, drawable paramDrawable2, Dra Wable ParamDrawable3, drawable paramDrawable4) {if (paramDrawable3! = null) This.dright = ParamDrawable3; Super.setcompounddrawables (ParamDrawable1, ParamDrawable2, ParamDrawable3, paramDrawable4);}}

XML layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <com.example.myedittexttest.myedittext android:id=" @+id/edit_text "Android:layout_w Idth= "Fill_parent" android:layout_height= "50DP" android:layout_margintop= "50DP" android:background= "#        88aaff "android:drawableright=" @drawable/edit_clear "android:textcursordrawable=" @null "/> <button        Android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_below= "@+id/edit_text" android:layout_margintop= "84DP" android:layout_torightof= "@+id/textview1" android:text= "button"/></Rela Tivelayout>


Introduction to Attributes in XML:

Display the X button on the right:

android:drawableright= "@drawable/edit_clear"

Set the cursor color setting @null indicates that the cursor color is the same as the font color of the input box

Show hidden cursor

Android:cursorvisible= "true"//Display android:cursorvisible= "false"//Hide

Please correct me where you are wrong.

Daily Diligence

Last God Beast Town Building

┏┓┏┓//┏┛┻━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//  ┃┃   god beast bless  ┃┃   Code no bug!  ┃┗━━━┓//  ┃┣┓//  ┃┏┛//  ┗┓┓┏━┳┓┏┛//    ┃┫┫┃┫┫//    ┗┻┛┗┻┛

Android Custom EditText input box with empty button

Related Article

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.