例子1:如图:当使用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<EditText> 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) {
}
}
}
例子2:布局中有ListView、ScrollView这种类似的可上下滑动控件,便会引起键盘的自动弹出(注意是上下滑动,不是左右滑动,HorizontalScrollView就不会引起键盘的自动弹出)。
经过测试:
1、在Manifest.xml中设定activity的属性 可行,但效果是“点击输入框后再收起键盘”。
android:windowSoftInputMode="stateHidden|stateUnchanged"
2、在其父控件下,添加如下的属性,就可以完美解决:
android:focusable="true"
android:focusableInTouchMode="true"