Android: cómo borrar un edittext mediante botón cruzado en el lado derecho

He creado un edittext para la búsqueda, que contiene en el lado izquierdo un icono de la búsqueda y en el lado derecho un icono de la cruz:

<EditText android:id="@+id/Search" android:layout_width="250dp" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/ic_menu_search" android:drawableRight="@android:drawable/ic_delete" android:hint="Search Product .." > </EditText> 

Quiero saber cómo puedo borrar el contenido de editText cuando hago clic en el botón de cruz. Gracias de antemano.

Una respuesta mejorada por @aristo_sh de Manejar los eventos de clic en un drawable dentro de un EditText

  mQueryEditText.setOnTouchListener(new OnTouchListener() { final int DRAWABLE_LEFT = 0; final int DRAWABLE_TOP = 1; final int DRAWABLE_RIGHT = 2; final int DRAWABLE_BOTTOM = 3; @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { int leftEdgeOfRightDrawable = mQueryEditText.getRight() - mQueryEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width(); // when EditBox has padding, adjust leftEdge like // leftEdgeOfRightDrawable -= getResources().getDimension(R.dimen.edittext_padding_left_right); if (event.getRawX() >= leftEdgeOfRightDrawable) { // clicked on clear icon mQueryEditText.setText(""); return true; } } return false; } }); 

Prefiero usar otro Edittex custome como abajo edittext despejable. Puede usarlo en xml como edittext normal. Para escuchar un evento claro, puede setListener para su ClearableEdittext

 /** Copyright 2014 Alex Yanchenko * To change clear icon, set * <p/> * <pre> * android:drawableRight="@drawable/custom_icon" * </pre> */ public class ClearableEditText extends EditText implements OnTouchListener, OnFocusChangeListener, TextWatcherAdapter.TextWatcherListener { public interface Listener { void didClearText(); } public void setListener(Listener listener) { this.listener = listener; } private Drawable xD; private Listener listener; public ClearableEditText(Context context) { super(context); init(); } public ClearableEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } @Override public void setOnTouchListener(OnTouchListener l) { this.l = l; } @Override public void setOnFocusChangeListener(OnFocusChangeListener f) { this.f = f; } private OnTouchListener l; private OnFocusChangeListener f; @Override public boolean onTouch(View v, MotionEvent event) { if (getCompoundDrawables()[2] != null) { boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - xD .getIntrinsicWidth()); if (tappedX) { if (event.getAction() == MotionEvent.ACTION_UP) { setText(""); if (listener != null) { listener.didClearText(); } } return true; } } if (l != null) { return l.onTouch(v, event); } return false; } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { setClearIconVisible(!TextUtils.isEmpty(getText())); } else { setClearIconVisible(false); } if (f != null) { f.onFocusChange(v, hasFocus); } } @Override public void onTextChanged(EditText view, String text) { if (isFocused()) { setClearIconVisible(!TextUtils.isEmpty(text)); } } private void init() { xD = getCompoundDrawables()[2]; if (xD == null) { xD = getResources() .getDrawable(android.R.drawable.presence_offline); } xD.setBounds(0, 0, xD.getIntrinsicWidth(), xD.getIntrinsicHeight()); setClearIconVisible(false); super.setOnTouchListener(this); super.setOnFocusChangeListener(this); addTextChangedListener(new TextWatcherAdapter(this, this)); } protected void setClearIconVisible(boolean visible) { Drawable x = visible ? xD : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], x, getCompoundDrawables()[3]); } } 

Prueba esto:

Activity_main.xml

 <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="9dp" android:padding="5dp"> <EditText android:id="@+id/Search" android:layout_width="250dp" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/ic_menu_search" android:hint="Search Product .." > </EditText> <Button android:id="@+id/clearText" android:layout_width="23dp" android:layout_height="23dp" android:layout_marginRight="10dp" android:layout_gravity="right|bottom" android:layout_marginBottom="10dp" android:background="@android:drawable/ic_delete" android:onClick="clear"/> </FrameLayout> 

MainActivity.java

 public class MainActivity extends AppCompatActivity { EditText mEditText; Button mClearText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.Search); mClearText = (Button) findViewById(R.id.clearText); //initially clear button is invisible mClearText.setVisibility(View.INVISIBLE); //clear button visibility on text change mEditText.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { //do nothing } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //do nothing } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.length() != 0) { mClearText.setVisibility(View.VISIBLE); } else { mClearText.setVisibility(View.GONE); } } }); } //clear button onclick public void clear(View view) { mEditText.setText(""); mClearText.setVisibility(View.GONE); } } 
  • Selector de fondo de botones
  • Android: imageView.getDrawable () devuelve null
  • Uso de ShapeDrawable predeterminado en un StateListDrawable en Android 4.2
  • ICS-buscando botones: ¿qué dibujable tengo que elegir en ICS SDK?
  • ¿Cómo se puede utilizar un marcador de posición de carga que es un spinner animado con cargadores de imágenes como Glide, Picasso etc?
  • Cambio de color en una forma dentro de una lista de capas mediante programación
  • Xamarin Android SetBackgroundDrawable obsoleta pero no SetBackground ()
  • Android List UI Style ListView con Barra de Acción Contextual?
  • XML dibujable compuesto de png y superposición
  • ¿Cómo obtiene un objeto Drawable de un id de recurso en el paquete android?
  • Dibujar complejo dibujable en api inferior a 23
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.