如何用返回按钮关闭滑动抽屉 - android

时间:2014-04-05 16:51:43

标签: android slidingdrawer

当我点击返回键时我尝试关闭滑动抽屉,但它不起作用 这是我使用的代码

@Override
public void onBackPressed() {
   Log.d("onBackPressed Called", FROM_SETTINGS_KEY);
      slidingDrawer.close();

}

xml:

<SlidingDrawer
    android:id="@+id/slidingDrawer1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="197dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>
</SlidingDrawer>

那么如何让返回键关闭这个滑动抽屉?

1 个答案:

答案 0 :(得分:1)

您将覆盖Activity.onKeyDownwhich Activity already overrides when implementing Activity.onBackPressed,然后返回false KeyEvent.KEYCODE_BACK,表示您尚未处理此事件,并且应该继续传播。< / p>

要解决问题,请停止覆盖Activity.onKeyDown。另外,如果Activity.onBackPressed未打开,请将super更改为致电SlidingDrawer

private SlidingDrawer slidingDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
}

@Override
public void onBackPressed() {
    if (slidingDrawer.isOpened()) {
        slidingDrawer.close();
    } else {
        super.onBackPressed();
    }
}