上下文动作条隐藏当我点击硬件后退按钮和键盘不在 [英] Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out
问题描述
我使用的语境动作条进行编辑,当我有显示的键盘,我想通过$ P $来隐藏它pssing硬件后退按钮,它隐藏了键盘,但它也取消上下文动作条,我真的can'找不到办法如何保持它。
I am using the Contextual ActionBar for editing and when I have the keyboard shown and I want to hide it by pressing the hardware back button, it hides the keyboard but it also cancels the contextual actionbar and I really can´t find a way how to keep it on.
有人吗?
推荐答案
您应该尽量覆盖返回键
硬件,并以<$ C处理预期的行为$ C>布尔如下:
You should try to override the Back Key
hardware, and handle the expected behaviour with a boolean
as follows:
// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;
// isVisible becomes 'true' if the user clicks on EditText
// then, if the user press the back key hardware, handle it:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// check isVisible
if(isVisible) {
// hide the keyboard
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
} else {
// remove the CAB
mActionMode.finish();
}
}
return false;
}
另一种解决方案可能是叫 dispatchKeyEvent
方法显示 CAB
时,它仍然是所谓的:
Another solution might be to call dispatchKeyEvent
method which is still called when the CAB
is displayed:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// check CAB active and isVisible softkeyboard
if(mActionModeIsActive && isVisible) {
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
return true;
// Maybe you might do not call the 'else' condition, anyway..
} else {
mActionMode.finish();
return true;
}
}
return super.dispatchKeyEvent(event);
}
这应该做的伎俩,但我没有测试它。希望这有助于。结果
的来源:<一个href=\"http://emumair.word$p$pss.com/2011/11/22/how-to-override-android-back-key-when-soft-keyboard-is-open/\"相对=nofollow>如何替代Android的后退键时softkeyboard是开放 - prevent取消操作模式由preSS后退按钮 的
This should do the trick, but I have not tested it. Hope this helps.
Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button
这篇关于上下文动作条隐藏当我点击硬件后退按钮和键盘不在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!