0

I have an EditText that I need to ignore Backspace keyEvents. I have the following class, but it doesn't work:

public class CustomEditText extends EditText    {

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context);
        // TODO Auto-generated constructor stub
        this.setOnKeyListener(new OnKeyListener() {                 
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
                if(keyCode == KeyEvent.KEYCODE_DEL){  
                    //do nothing
                }
                return true;
            }
        });
    }
}
frazer
  • 426
  • 2
  • 6
  • 18

3 Answers3

1

Try overriding the onKeyDown method instead of adding a listener

Jokahero
  • 1,074
  • 10
  • 21
  • this works if the field is empty, but otherwise the event never reaches the onKeyDown method – frazer Jun 04 '12 at 19:43
1

Try returning false here: if(keyCode == KeyEvent.KEYCODE_DEL){
return false; }

Medo
  • 671
  • 4
  • 11
0

Chedk out the InputFilter, it can probably do this

https://stackoverflow.com/a/4401227/614231

I have used the InputFilter to do other types of filtering on the input, but I'm not sure if you can actually use it to prevent the backspace character or not.

Community
  • 1
  • 1
stuckless
  • 6,515
  • 2
  • 19
  • 27