如图:当使用EditText输入框时,总会自动弹出系统自带键盘。很多时候我们需要的是根据自定义控件输入改写输入框内容,因为需要把自带键盘屏蔽。
网上很多屏蔽系统键盘的方法无非是以下几种:
方法一:
在 AndroidMainfest.xml中选择activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden
< activity android:name=".Main"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden" />
方法二:
让 EditText失去焦点,使用EditText的clearFocus方法
EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:
EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
经过实验,均无法实现需求效果。
解决方法很简单,代码如下:
/**
* 禁止Edittext弹出软件盘,光标依然正常显示。
*/
public void disableShowSoftInput() {
if (android.os.Build.VERSION.SDK_INT <= 10) {
editText.setInputType(InputType.TYPE_NULL);
} else {
Class cls = EditText.class;
Method method;
try {
method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
}
try {
method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
}
}
}