android - 按下 handle 以不关闭 Activity

标签 android android-activity onbackpressed

我想像 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/

相关文章:

android - 按下后退按钮时如何在android中停止异步任务

java - 不要在 Activity 销毁时销毁绑定(bind)服务

android - 单击后退按钮两次以使用 rxjava 退出 Activity

android-layout - android 列出两列图像

Android 启动器图标未显示

Android:如何用一根手指旋转 View

java - 我正在使用列表和 OnItemClickListener,但是当我单击该项目时,它不会去任何地方

android - FragmentPagers 中的 fragment 交互监听器

android - 当 alertdialog 的 setcancelable 为 false 时不工作 onbackpressed

android - 如何在 android fragment 中执行 onBackPress 对话框?