因需自定义键盘,系统键盘则需要隐藏,但是EditText的光标又需要存在~~~
尝试了n中属性配置,依然不得解,百度n种,依然无效,可想我内心是多么奔溃的,一个小功能,耗费如此精力😢
最终 :皇天不负有心人啊,查阅各种,终于解决
/**
* 强制隐藏系统软键盘,不够EditText是否点击都隐藏
*
* @param v
* @param context
*/
public static void HideKeyboard(EditText v, Activity context) {
context.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
int currentVersion = android.os.Build.VERSION.SDK_INT;
String methodName = null;//4.0以上和4.2以上方法名有所改变
if (currentVersion >= 16) {
// 4.2
methodName = "setShowSoftInputOnFocus";
} else if (currentVersion >= 14) {
// 4.0
methodName = "setSoftInputShownOnFocus";
}
if (methodName == null) {//4.0以下采用此方法
v.setInputType(InputType.TYPE_NULL);
} else {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
try {
setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(v, false);
} catch (NoSuchMethodException e) {
v.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();
}
}
}