Android ActivityGroup 内后退按钮的使用

发布于 2024-12-26 19:21:18 字数 440 浏览 7 评论 0原文

我正在使用 TabHost 开发一个应用程序。我正在使用 android 默认后退按钮通过覆盖每个选项卡的 ActivityGroup 内的 onBackPressed() 方法从当前活动返回到上一个活动。

现在,问题是,在我的一项活动中,我有一个 EditText,它在活动开始时获得焦点。然后,如果我按返回,它不会转到上一个活动,而是关闭应用程序。通过在互联网上搜索问题,我发现当 EditText 获得焦点(它是活动视图的子视图)时,活动会失去焦点,然后如果按下后退按钮,由于缺乏对当前活动的关注,它会关闭应用。我仍然有点困惑或者可以说不清楚这个问题。

因此,无论如何,我已经设法使用代码在运行时设置和删除对 EditText 的焦点。但现在,由于 EditText 没有焦点,如果按下后退按钮,则会关闭应用程序。我真的很困惑到底发生了什么。因此,如果有人对这个问题有任何想法或解决方案,请帮助解决这个问题。我将非常感激。谢谢。

I am developing an application using TabHost. I am using android default back button to move back to previous activity from current activity by overriding onBackPressed() method inside ActivityGroup of each tab.

Now , the problem is , in one of my activity i have an EditText which get focused when the activity starts. Then , if i press back , it does not go to previous activity , instead it closes the application. By searching about the problem on internet what i have found is that when the EditText get focused which is a child view of activity's view , the activity loss focus and then if back button is pressed , for lack of focus on the current activity it closes the application. Still i am little bit confused or can be say not clear about the problem.

So, any how , i have managed to set and remove focus over EditText on run time using code. But still now , as the EditText does not have focus , if the back button is pressed , it closes the application. I am really confused what's actually going on. So , if anyone have any idea or solution about the problem , please help on the issue. I will appreciate that very much. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2025-01-02 19:21:18

您可以通过将 Key Listener 添加到 EditText 来覆盖此行为。试试这个,

    name_edit.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Log.i("Back event Trigered", "Back event");
                activitygroup.back();
            }
            return false;
        }
    });

You can override this behavior by adding Key Listener to your EditText. Try this out,

    name_edit.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Log.i("Back event Trigered", "Back event");
                activitygroup.back();
            }
            return false;
        }
    });
鹿童谣 2025-01-02 19:21:18

试试这个..

@Override
    public void onBackPressed() {
        onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
        super.onBackPressed();
    }

try this..

@Override
    public void onBackPressed() {
        onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
        super.onBackPressed();
    }
十六岁半 2025-01-02 19:21:18

试试这个

 public void onBackPressed() {

      startActivity(new Intent(currentActivity.this, previousActivity.class));
        finish();
    }

try this

 public void onBackPressed() {

      startActivity(new Intent(currentActivity.this, previousActivity.class));
        finish();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文