由于自己写了个键盘在界面,所以在编辑editText时,并不需要弹出系统的输入法,因此如何让他隐藏起来是个问题,试了下网上的方法,但似乎都不能正常的隐藏,并让他还能获得焦点;
查找了相关资料,发现可以利用反射,将该控件的输入法隐藏,代码如下:
/**
* 禁止Edittext弹出软件盘,光标依然正常显示。
*/
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) {
// TODO: handle exception
}
try {
method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
// TODO: handle exception
}
}
}