質問

i try to close the sliding drawer when i click on the return back key but its not working here is the code i used

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

}

the 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>

so how can i make the return back key close this sliding drawer ?

役に立ちましたか?

解決

You're overriding Activity.onKeyDown, which Activity already overrides when implementing Activity.onBackPressed, then returning false for KeyEvent.KEYCODE_BACK which indicates that you have not handled this event and it should continue to be propagated.

To fix your problem stop overriding Activity.onKeyDown. Also, change your Activity.onBackPressed to call super if the SlidingDrawer isn't opened.

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();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top