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

3 回答 3

1

尝试覆盖onKeyDown方法而不是添加监听器

于 2012-06-04T14:52:57.783 回答
1

尝试在此处返回 false:if(keyCode == KeyEvent.KEYCODE_DEL){
return false; }

于 2012-06-04T14:54:13.350 回答
0

查出来InputFilter,它可能可以做到这一点

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

我已经使用 对InputFilter输入进行其他类型的过滤,但我不确定您是否可以实际使用它来防止退格字符。

于 2012-06-04T14:49:30.223 回答