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


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

 
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"

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值