监听按键,删除

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

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

你可能感兴趣的:(监听按键,删除)