简体   繁体   English

如何在 Android 中的 BackPress 上关闭 Drawer 布局?

[英]How to close Drawer layout on BackPress in Android?

I press the navigation drawer, then if I press back button, the app exits rather than returning to the previous activity.我按下导航抽屉,然后如果我按下后退按钮,应用程序将退出而不是返回到上一个活动。 If I change the xml file, then this problem doesn't occur.如果我更改 xml 文件,则不会出现此问题。 So I think the problem is in the xml file.所以我认为问题出在xml文件中。 Can anyone tell me what is the problem?谁能告诉我是什么问题? Here's the xml code.`这是xml代码。`

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="500dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/blue_train" />

<TextView
    android:id="@+id/trainName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="200dp"
    android:layout_marginLeft="14dp"
    android:text="Train Name"
    android:textColor="@color/bluedark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/etName"
    android:layout_width="150dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/trainName"
    android:layout_alignBottom="@+id/trainName"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/trainName"
    android:background="@drawable/line"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/getS"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etName"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="@drawable/button2"
    android:text="Get Train Schedule"
    android:textColor="@color/white" />

` `

This will close the drawer when it's open and back is pressed rather than taking you back to the previous activity (or exiting).这将在抽屉打开并按下返回时关闭抽屉,而不是带您返回上一个活动(或退出)。

DrawerLayout drawer...

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    if(drawer.isDrawerOpen(Gravity.LEFT)){
        drawer.closeDrawer(Gravity.LEFT);
    }else{
        super.onBackPressed();
    }
}
ListView mDrawerList;

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

if(mDrawerLayout.isDrawerOpen(mDrawerList)){
    mDrawerLayout.closeDrawer(mDrawerList);
}else{
    super.onBackPressed();
}
}

I have done this way:我已经这样做了:

private DrawerLayout mDrawerLayout;

onCreate() : onCreate()

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

Hope this will help you.希望这会帮助你。

@Hiren Patel's answer is the most elegant so far, but you should set: @Hiren Patel 的回答是迄今为止最优雅的,但您应该设置:

mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

Or或者

mDrawerLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS

Just so you don't block the other child views from receiving focus, and thus blocking the soft keyboard for instance.只是这样您就不会阻止其他子视图接收焦点,从而阻止软键盘。

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

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