我想像 whatsapp 一样处理后退键,如果用户按下回键,则在键入时键盘被关闭,如果用户再次按下键,我们将转到聊天选项卡。在我的 Activity 中,我希望当用户在键入时按下回键键盘关闭并处理一些 UI 元素。我尝试在 onBackPressed() 方法中处理它,但它不起作用并且 Activity 被终止。
edittext.setOnEditorActionListener (
new EditText.OnEditorActionListener () {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event != null &&
event.getAction () == KeyEvent.ACTION_DOWN &&
event.getKeyCode () == KeyEvent.KEYCODE_ENTER) {
if (event == null || !event.isShiftPressed ()) {
// the user is done typing.
View view1 = getCurrentFocus ();
imm.hideSoftInputFromWindow (view1.getWindowToken (), 0);
Log.e (TAG, "TYPING DONE ");
result = tv_result.getText ().toString();
return true; // consume.
}
}
return false; // pass on to other listeners.
}
}
);
onBackPressed() 代码
@Override
public void onBackPressed() {
tv_result.setText("Some text");
}
最佳答案
由于您没有提供任何代码,而且我还不能发表评论,我假设您让 onBackPressed 执行默认情况下的操作,这意味着在您的 onBackPressed()
方法你有 super.onBackPressed();
,删除该行应该可以解决问题
编辑
由于您已经使用代码更新了您的问题,因此在 super.onBackPressed() 之后写的任何内容都不会在视觉上看到,因为 super.onBackPressed()
返回到上一个 Activity 。移除 super.onBackPressed()
,并使用 if else 语句处理第二次后退
关于android - 按下 handle 以不关闭 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49796532/