转自:http://bbs.csdn.net/topics/390498445
当为隐藏系统键盘为 EditText 设置 setInputType(InputType.TYPE_NULL)后EditText无法正常获取光标
可对EditText进行如下设置:
public void hideSoftInputMethod(EditText ed){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
int currentVersion = android.os.Build.VERSION.SDK_INT;
String methodName = null;
if(currentVersion >= 16){
// 4.2
methodName = "setShowSoftInputOnFocus";
}
else if(currentVersion >= 14){
// 4.0
methodName = "setSoftInputShownOnFocus";
}
if(methodName == null){
ed.setInputType(InputType.TYPE_NULL);
}
else{
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
try {
setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(ed, false);
} catch (NoSuchMethodException e) {
ed.setInputType(InputType.TYPE_NULL);
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文介绍了一种在Android中为EditText设置setInputType(InputType.TYPE_NULL)后导致无法正常显示光标的问题解决方案。通过调整软键盘显示模式并根据不同Android版本使用特定的方法来禁用软键盘的自动弹出。

518

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



