Android 禁止 EditText 弹出软件盘
Android 大约 1859 字禁止弹出
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<EditText> 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;
}
});
阅读 4934 · 发布于 2019-04-09
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Spring Boot 单元测试 @MockBean 模拟依赖注入阅读 1253
-
Android 自定义底部弹出对话框阅读 5630
-
Linux CentOS 内核版本 2.6 升级为 4.4阅读 4406
-
Linux Shell 符号及变量阅读 2421
-
Prometheus+Grafana+blackbox_exporter 监控 HTTP TCP PING ICMP阅读 2697
-
Oracle ORA-01502: index 'INDEX_NAME' or partition of such index is in unusable state阅读 2567
-
MySQL 聚合函数阅读 1169
-
算法:插入排序阅读 864
-
PostgreSQL 查询表中的自增序列名称阅读 5042
-
软考-系统架构设计师:逆向工程阅读 1958