简体   繁体   中英

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) 在此处输入图片说明

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. 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. This can be achieved by overriding the onKeyDown function, as follows:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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