Android custom EditText input box with the clear button, androidedittext

Source: Internet
Author: User

Android custom EditText input box with the clear button, androidedittext

Summary: The Android custom EditText input box contains the Clear button.

 

After the user enters the character, EditText will automatically display the delete button on the right side of the input box.

Rewrite EditText to simplify the layout.

:

 

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"; private Drawable dRight; p Rivate Rect rBounds; public MyEditText (Context 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 ();} // initialize the edittext control privat E void initEditText () {setEditTextDrawable (); addTextChangedListener (new TextWatcher () {// listen for text content changes @ Overridepublic void afterTextChanged (Editable paramEditable) {}@ Overridepublic void substring (CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {}@ reserved void onTextChanged (CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {DitText. this. setEditTextDrawable () ;}}) ;}// controls the public void setEditTextDrawable () {if (getText (). toString (). length () = 0) {setCompoundDrawables (null, null);} else {setCompoundDrawables (null, null, this. dRight, null) ;}@ Overrideprotected void onDetachedFromWindow () {super. onDetachedFromWindow (); this. dRight = null; this. rBounds = null;}/*** after clicking Add touch event, the effect of clearing editText is displayed */@ Overridepubl Ic boolean onTouchEvent (MotionEvent paramMotionEvent) {if (this. dRight! = Null) & (paramMotionEvent. getAction () = 1) {this. rBounds = this. dRight. getBounds (); int I = (int) paramMotionEvent. getRawX (); // distance from the screen // int I = (int) paramMotionEvent. getX (); // distance from the border if (I> getRight ()-3 * this. rBounds. width () {setText (""); paramMotionEvent. setAction (MotionEvent. ACTION_CANCEL) ;}} return super. onTouchEvent (paramMotionEvent);}/*** display the top left and bottom right of the X image on the right */@ Overridepublic void setC OmpoundDrawables (Drawable paramDrawable1, Drawable paramDrawable2, Drawable 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:paddingBottom="@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_width="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" /></RelativeLayout>


Introduction to attributes in XML:

The X button on the right is displayed:

android:drawableRight="@drawable/edit_clear"

 

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

android:textCursorDrawable="@null" 

 

Show Hidden cursor

Android: cursorVisible = "true" // display android: cursorVisible = "false" // hide

 

Correct the error

Daily Improvement

Finally, zombith town building

// Please wait until then // zookeeper // zookeeper without any bugs! // Zookeeper has been completed before // zookeeper has been executed before being executed ┛ ┗ ┻ ┛

 


How does one add the clear text button in EditText when the android EditText clears text?

Put EditText, ImageView, and ImageView attributes in FrameLayout. android: layout_gravity = "right"
EditText sets the setOnFocusChangeListener listener, and sets the ImageView VISIBLE/GONE according to whether the EditText gets the focus.
Finally, the ImageView click event listening is completed. EditText setText ("");

The EditText content of Android is cleared.

Yes. setText () is used ().
You can set the layout in xml, for example, in main. xml: <TextView android: text = "Initial Value" android: id = "cnumber"/>
You can also:
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final EditText et_number = (EditText) findViewById (R. id. cnumber );
Et_number.setText ("Modify value ");
// GetText (); get the value

}
 

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.