在开发过程中,使用自定义软键盘时要隐藏系统自带的键盘,但常规方法有一个弊端是隐藏键盘的同时会让EditText的光标消失,以下为解决方法(在Activity中):
public void hide(View v){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
附隐藏键盘方法:
final EditText editText2=(EditText)findViewById(R.id.no2);
editText2.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT <= 10) {
// 点击EditText,屏蔽默认输入法
editText2.setInputType(InputType.TYPE_NULL); // editText是声明的输入文本框。
} else {
// 点击EditText,隐藏系统输入法
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method method = cls.getMethod("setShowSoftInputOnFocus",
boolean.class);// 4.0的是setShowSoftInputOnFocus,4.2的是setSoftInputShownOnFocus
method.setAccessible(false);
method.invoke(editText2, false); // editText是声明的输入文本框。
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return false;
}
});
}