在不同的sdk版本下所用到的方法也不一样,所以要先判断sdk的版本是否低于10,如果在10以下可以直接用过editText.setInputType(InputType.TYPE_NULL);来设置不弹出软键盘
在
public void disableShowSoftInput(EditText editText) {
if (android.os.Build.VERSION.SDK_INT <= 10) {
editText.setInputType(InputType.TYPE_NULL);
} else {
Class<EditText> cls = EditText.class;
Method method;
try {
method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
e.printStackTrace();
}
try {
method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Android中EditText在点击的时候不弹出系统键盘
适配不同SDK版本禁用软键盘显示
最新推荐文章于 2024-04-28 08:43:59 发布
该博客介绍了如何根据Android系统的SDK版本来决定是否禁用EditText的软键盘显示。在SDK版本10及以下,直接使用InputType.TYPE_NULL;对于更高版本,通过反射调用setShowSoftInputOnFocus和setSoftInputShownOnFocus方法来实现相同效果,避免了因版本差异导致的兼容性问题。
1391

被折叠的 条评论
为什么被折叠?



