se necesita hacer Custom EditText, Make Extends EditText y @Override Below Method en él.
Aquí hay un código de ejemplo.
private ArrayList<Integer> keysToIgnore = new ArrayList<Integer>();
public EditTextCus(Context context) {
super(context);
}
public EditTextCus(Context context, AttributeSet attrs){
super(context, attrs);
}
public EditTextCus(Context context, AttributeSet attrs, int defStyle){
super (context, attrs, defStyle);
}
private Boolean keyInIgnoreList(int keyCode) {
for (Integer i:keysToIgnore){
int key = i.intValue();
if (key == keyCode)
return true;
}
return false;
}
public void addKeyToIgnoreList(int keyCode) {
if (!keyInIgnoreList(keyCode)){
keysToIgnore.add(keyCode);
}
}
public void removeKeyFromIgnoreList(int keyCode) {
if (keyInIgnoreList(keyCode)){
Integer key=new Integer(keyCode);
keysToIgnore.remove(key);
}
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
//Log.d("ws", "dispatchKeyEventPreIme(" + event + ")");
// swallow the any key in the ignore list
if (keyInIgnoreList(event.getKeyCode())) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
/*&& event.getRepeatCount() == 0*/) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
return true;
}
}
}
}
return super.dispatchKeyEventPreIme(event);
}
también se puede hacer esto a la clave de búsqueda:
mCustomEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_SEARCH && event.getAction()==KeyEvent.KEYCODE_SEARCH){
System.out.println("Search pressed.........");
}
return false;
}
});
Eso es malo. Supongo que será necesario verificar si hay una cadena vacía. Elegiré esta respuesta porque es la respuesta real a mi pregunta, no solo las alternativas. – Pieter888