最小化键盘,而不是在按返回时返回到上一个 activity
Minimize keyboard instead of going back to previous activity on press back
嗨,我正在构建键盘应用程序,当我想最小化键盘时它会返回到之前的 activity。例如如果我在 whatsapp 上与朋友聊天并想在按下后退按钮时最小化键盘,它将返回到聊天列表而不是最小化。所以我需要的是当按下后退按钮时它应该最小化键盘而不是返回到之前的 activity。
如果可以请帮忙
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case 4:
if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
return PROCESS_HARD_KEYS;
}
case 66:
return false;
case 67:
if (this.mComposing.length() > 0) {
onKey(-5, null);
return PROCESS_HARD_KEYS;
}
break;
default:
if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.clearMetaKeyStates(2);
keyDownUp(29);
keyDownUp(42);
keyDownUp(32);
keyDownUp(46);
keyDownUp(43);
keyDownUp(37);
keyDownUp(32);
return PROCESS_HARD_KEYS;
}
}
if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
return PROCESS_HARD_KEYS;
}
}
return super.onKeyDown(keyCode, event);
}
只是替换这个希望它能起作用。
public boolean onKeyDown(int keyCode, KeyEvent event, View view) {if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// hide keyboard here
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}
switch (keyCode) {
case 4:
if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
return PROCESS_HARD_KEYS;
}
case 66:
return false;
case 67:
if (this.mComposing.length() > 0) {
onKey(-5, null);
return PROCESS_HARD_KEYS;
}
break;
default:
if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.clearMetaKeyStates(2);
keyDownUp(29);
keyDownUp(42);
keyDownUp(32);
keyDownUp(46);
keyDownUp(43);
keyDownUp(37);
keyDownUp(32);
return PROCESS_HARD_KEYS;
}
}
if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
return PROCESS_HARD_KEYS;
}
}
return super.onKeyDown(keyCode, event);
}
嗨,我正在构建键盘应用程序,当我想最小化键盘时它会返回到之前的 activity。例如如果我在 whatsapp 上与朋友聊天并想在按下后退按钮时最小化键盘,它将返回到聊天列表而不是最小化。所以我需要的是当按下后退按钮时它应该最小化键盘而不是返回到之前的 activity。 如果可以请帮忙
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case 4:
if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
return PROCESS_HARD_KEYS;
}
case 66:
return false;
case 67:
if (this.mComposing.length() > 0) {
onKey(-5, null);
return PROCESS_HARD_KEYS;
}
break;
default:
if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.clearMetaKeyStates(2);
keyDownUp(29);
keyDownUp(42);
keyDownUp(32);
keyDownUp(46);
keyDownUp(43);
keyDownUp(37);
keyDownUp(32);
return PROCESS_HARD_KEYS;
}
}
if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
return PROCESS_HARD_KEYS;
}
}
return super.onKeyDown(keyCode, event);
}
只是替换这个希望它能起作用。
public boolean onKeyDown(int keyCode, KeyEvent event, View view) {if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// hide keyboard here
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}
switch (keyCode) {
case 4:
if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
return PROCESS_HARD_KEYS;
}
case 66:
return false;
case 67:
if (this.mComposing.length() > 0) {
onKey(-5, null);
return PROCESS_HARD_KEYS;
}
break;
default:
if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.clearMetaKeyStates(2);
keyDownUp(29);
keyDownUp(42);
keyDownUp(32);
keyDownUp(46);
keyDownUp(43);
keyDownUp(37);
keyDownUp(32);
return PROCESS_HARD_KEYS;
}
}
if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
return PROCESS_HARD_KEYS;
}
}
return super.onKeyDown(keyCode, event);
}