frazer
frazer

Reputation: 426

Ignore backspace in EditText

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;
            }
        });
    }
}

Upvotes: 0

Views: 2443

Answers (3)

Medo
Medo

Reputation: 671

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

Upvotes: 1

Jokahero
Jokahero

Reputation: 1074

Try overriding the onKeyDown method instead of adding a listener

Upvotes: 1

stuckless
stuckless

Reputation: 6545

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.

Upvotes: 0

Related Questions