android关闭系统弹窗,Android 禁止 EditText 弹出软件盘

禁止弹出

21版本后有方法mEditText.setShowSoftInputOnFocus(false);可以设置不显示

21版本之前采用反射的方式获取方法名,然后调用

public void hideSoftInputMethod(Activity a, EditText editText) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

mDigitsLayout.mEditText.setShowSoftInputOnFocus(false);

return;

}

a.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

int currentVersion = Build.VERSION.SDK_INT;

String methodName = null;

if (currentVersion >= Build.VERSION_CODES.JELLY_BEAN) {// 4.2

methodName = "setShowSoftInputOnFocus";

} else if (currentVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {// 4.0

methodName = "setSoftInputShownOnFocus";

}

if (methodName == null) {

editText.setInputType(InputType.TYPE_NULL);

} else {

Class cls = EditText.class;

Method setShowSoftInputOnFocus;

try {

setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);

setShowSoftInputOnFocus.setAccessible(true);

setShowSoftInputOnFocus.invoke(editText, false);

} catch (NoSuchMethodException e) {

editText.setInputType(InputType.TYPE_NULL);

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

不自动弹出

使EditText进入界面默认不跳出软键盘(不获得焦点)

给EditText的父控件设置属性:

android:focusable="true"

android:focusableInTouchMode="true"

点击其他区域收回软键盘

点击EditText区域外让EditText失去焦点

给父布局设置触摸监听,设置focusable为true,设置focusable的触摸模式为true,最后请求焦点

mLinearLayout.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mLinearLayout.setFocusable(true);

mLinearLayout.setFocusableInTouchMode(true);

mLinearLayout.requestFocus();

return false;

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值