监听按键,删除

2018-03-29  本文已影响0人  跟我去北方吧

点击监听返回按键逐字删除

editText.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
                        Editable editable = editText.getText();
                        int index = editText.getSelectionStart();
                        String passwd = editText.getText().toString();
                        Log.e(TAG, "KeyCode = " + keycode);
                        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                            if (keycode == KeyEvent.KEYCODE_BACK || keycode == KeyEvent.KEYCODE_DEL
                                    || keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
                                if (passwd.isEmpty() || index == 0) {

                                } else {
                                    editable.delete(index - 1, index);
                                    return true;
                                }
                            }

                        }

                        return false;
                    }
                });
上一篇下一篇

猜你喜欢

热点阅读