Android:EditText 软键盘退出监听解决方法

1、第一种

当hasFocus值为false时表示点击了别的控件,离开当前editText控件

mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
            
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                
            }
        });


2、第二种

关于 edittext 软键盘退出监听解决办法

  edittext 有个onCreateInputConnection 的方法,通过它可以自定义一个输入法连接器,那里连接器里面有个方法(finishComposingText)能监听输入完成的动作


    public class MyEditText extends EditText {
        
        public MyEditText(Context context) {
            super(context);
        }
        
        private OnFinishComposingListener mFinishComposingListener;
        
        public void setOnFinishComposingListener(OnFinishComposingListener listener) {
            this.mFinishComposingListener = listener;
        }
        
        @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            return new MyInputConnection(super.onCreateInputConnection(outAttrs), false);
        }
        
        public class MyInputConnection extends InputConnectionWrapper {
            public MyInputConnection(InputConnection target, boolean mutable) {
                super(target, mutable);
            }
            
            @Override
            public boolean finishComposingText() {
                boolean finishComposing = super.finishComposingText();
                if (mFinishComposingListener != null) {
                    mFinishComposingListener.finishComposing();
                }
                return finishComposing;
            }
        }
        
        public interface OnFinishComposingListener {
            public void finishComposing();
        }
    }


你可能感兴趣的:(Android)