Android屏蔽软键盘并且有光标显示。和关闭软键盘方法,保存下来方便日后使用

来源:互联网 发布:张大奕的淘宝店链接 编辑:程序博客网 时间:2024/06/04 00:23

屏蔽系统软键盘,并且有光标显示

 
private void closeSystemInput() {
InputMethodManager inputMethodManager =
 (InputMethodManager)mActivity
.getSystemService(mActivity.INPUT_METHOD_SERVICE);
 inputMethodManager.
 hideSoftInputFromWindow(mEdit.getWindowToken(), 0);
if (android.os.Build.VERSION.SDK_INT <= 10) {
mEdit.setInputType(InputType.TYPE_NULL);
}else{
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                    Class<EditText> cls = EditText.class;
                    Method setSoftInputShownOnFocus;
                    setSoftInputShownOnFocus = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
                    setSoftInputShownOnFocus.setAccessible(true);
                    setSoftInputShownOnFocus.invoke(mEdit, false);
            } catch (Exception e) {
                    e.printStackTrace();
            }
            try {
                    Class<EditText> cls = EditText.class;
                    Method setShowSoftInputOnFocus;
                    setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                    setShowSoftInputOnFocus.setAccessible(true);
                    setShowSoftInputOnFocus.invoke(mEdit, false);
            } catch (Exception e) {
                    e.printStackTrace();
            }
}

}

 关闭软键盘的方法


private void closeInput() {
InputMethodManager imm = (InputMethodManager) mActivity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (mActivity.getCurrentFocus() != null) {
if (mActivity.getCurrentFocus().getWindowToken() != null) {
imm.hideSoftInputFromWindow(mActivity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}

//监听软键盘弹出,在Android中貌似没有找到更好的监听软键盘弹出更好的方法,只能自定义layout监听其尺寸有变

public class MyScrollView extends ScrollView{

public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}


public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);

}
public MyScrollView(Context context) {
super(context);

}


public void setOnResizeListener(OnResizeListener l) {  
        mListener = l;  
    }  
  
    @Override  
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
        super.onSizeChanged(w, h, oldw, oldh);  
        if (mListener != null) {  
            mListener.OnResize();  
        }  
    }  
  
    private OnResizeListener mListener;  
  
    public static interface OnResizeListener {  
        void OnResize();  
    };  
}

//如遮挡住EditText 则在manifast对应的Activity设置属性

android:windowSoftInputMode="stateHidden|adjustResize"

注:以上内容均为转载。只供自己学习以及记忆方便

0 0