是否可以阻止用户关闭软键盘。换句话说,当我的活动可见时,屏幕上可以使用键盘(在任何时候)。这可能吗?如果是这样,你会如何实现它?
答案 0 :(得分:0)
您需要覆盖以下onKeyUp()
和onKeyDown()
方法,
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
return super.onKeyDown(keyCode, event);
}
您也可以尝试使用OnEditorActionListener
,使用EditText txtMyEdit = (EditText) findViewById(R.id.txtEdit);
txtMyEdit.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
// your additional processing...
return true;
}
else
{
return false;
}
}
});
,查看以下代码,
{{1}}
答案 1 :(得分:0)
这会强制键盘显示而不管焦点如何:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourViewObject.getWindowToken(), 0);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);