美文网首页
android监听虚拟键盘的退格键

android监听虚拟键盘的退格键

作者: Tenny1225 | 来源:发表于2015-12-05 16:05 被阅读731次

    什么都不说了,直接上代码

    public class XZEditView extends EditText {
        public XZEditView(Context context) {
            super(context);
        }
    
        public XZEditView(Context context, AttributeSet attrs) {
            super(context, attrs);
           // setText(" ");
        }
    
        public XZEditView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public interface OnBackspacePressListener {
             void onBackspacePressed();
        }
    
        private OnBackspacePressListener backspaceListener;
    
    
    
        public void setOnBackspacePressListener(OnBackspacePressListener backspaceListener) {
            this.backspaceListener = backspaceListener;
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_DEL) {
                if(backspaceListener != null) {
                    backspaceListener.onBackspacePressed();
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    
        @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            outAttrs.actionLabel = null;
            outAttrs.inputType = InputType.TYPE_NULL;
    
            BaseInputConnection connection = new BaseInputConnection(this, false) {
    
                @Override
                public String getTextBeforeCursor(int ignore, int ignore2) {
                    return " ";
                }
            };
            return connection;
        }
    }
    

    只需要实现OnBackspacePressListener 接口,就可以在onBackspacePressed方法中监听了

    相关文章

      网友评论

          本文标题:android监听虚拟键盘的退格键

          本文链接:https://www.haomeiwen.com/subject/adnshttx.html