简体   繁体   English

更改系统后退按钮方向

[英]Change System Back Button Orientation

I'm currently develop my activity, when you click on the back button, instead of slide down the keyboard, it will exit the activity (the normal back button behaviour). 我目前正在开发活动,当您单击后退按钮时,而不是向下滑动键盘,它将退出活动(正常的后退按钮行为)。 This is good. 很好

Now, the problem I have is, the System Back Button (on Nexus device) when the Keyboard is Shown is pointing down instead of point left (going back activity) 现在,我遇到的问题是,显示键盘时,系统后退按钮(在Nexus设备上)指向下而不是指向左(返回活动) 在此处输入图片说明

Show an example of keyboard point left. 显示键盘左键示例。

在此处输入图片说明

Is there a way to programmatically control and ensure it doesn't point down when the keyboard is up? 有没有一种方法可以通过编程控制并确保在键盘弹起时不会指向下方?

That actually isn't good. 那实际上是不好的。 The normal behavior of the back button on Android when the keyboard is up is to hide the keyboard. 按下键盘时,Android上后退按钮的正常行为是隐藏键盘。 If I hit the back button with the keyboard up to hide it and lost what I was doing, I'd be very pissed and consider your app buggy. 如果我在按下键盘的同时按下后退按钮将其隐藏起来而丢失了正在做的事情,那我会非常生气,并认为您的应用有问题。 In fact my assumption wouldn't be "they overrode the back button", it would be "the shitty app crashed". 实际上,我的假设不是“它们覆盖了后退按钮”,而是“糟糕的应用程序崩溃了”。

There is no way to programmatically change the direction of that button. 无法以编程方式更改该按钮的方向。 The downward arrow means hitting it will hide the keyboard. 向下箭头表示点击将隐藏键盘。 If the keyboard isn't hidden because your app broke the usual rules, then a downwards arrow is correct- the next hit of the back button will close the keyboard. 如果由于您的应用违反了常规规则而没有隐藏键盘,则向下箭头是正确的-再次按下后退按钮将关闭键盘。

Indeed, you can override the back button in Android. 实际上,您可以覆盖Android中的“后退”按钮。 This can be achieved by overriding the onKeyDown function, as follows: 这可以通过重写onKeyDown函数来实现,如下所示:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Hide keyboard and activate previous activity
        return false;
    }

    return super.onKeyDown(keyCode, event);
}

(Although I do admit, I've never tested this to see if it would work whilst the keyboard is open...) (尽管我承认,但我从未测试过此功能是否可以在键盘打开的情况下使用它。)

Hell, if this doesn't work you could even go about doing something slightly "hacky", like this: 天哪,如果这不起作用,您甚至可以做些“变态”的事情,像这样:

boolean opened = true;

public void setListenerToRootView(){
    final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100 )
                opened = true;
            else if(opened == true)
                opened = false;
        }
    });
}

This approach obviously may cause errors if they have a floating keyboard or something specific, but you can handle it as necessary. 如果这种方法具有浮动键盘或某些特定功能,则显然可能会导致错误,但是您可以根据需要进行处理。

Note: None of this solves the fact that the arrow points downward. 注意:所有这些都不能解决箭头指向下方的事实。 I don't think you can really modify this, though you can at least modify its behavior, by navigating to the previous activity whenever the keyboard has been removed. 我认为您无法真正修改它,尽管您至少可以通过在每次卸下键盘时导航至上一个活动来修改其行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM