clear focus on content when navigation drawer is showed

旧街凉风 提交于 2019-12-10 09:29:43

问题


What would be the best way to clear the focus in an android activity having a navigation drawer.

The problem it's that when the user has a textbox selected, if you open the navigation drawer the back event it's not consumed by the drawer, and the navigation goes back one step leaving the drawer opened.

Right now I solved using this on the activity, but i would like to know if someone use another solution where i don't need to use the ActionBarDrawerToggle events.

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
    View window = findViewById(R.id.content_frame);
    window.clearFocus();
}

回答1:


Indeed it seems the drawer doesn't have focus when it's opened so it's onKeyUp won't be called. I checked this in the host activity and in onBackPressed DrawerLayout#hasFocus() returns false if the activity has a view that gained focus - an EditText or something else.

I even tried to mess up with its descendantFocusability property and that didn't work. I managed to fix this with a similar approach as you did: the drawer layout is requesting the focus:

mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
    @Override
    public void onDrawerOpened(View drawerView) {
        mDrawerLayout.requestFocus();
    }
});

You are clearing the focus from content ViewGroup, but I am not sure who gets the focus then, most probably no view.
Honestly speaking, this seems to me a bug in DrawerLayout as if it's opened, it should have the focus gained.



来源:https://stackoverflow.com/questions/19571279/clear-focus-on-content-when-navigation-drawer-is-showed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!