[英]how to close the slidind drawer with the return button - android
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 : 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.您正在覆盖
Activity.onKeyDown
, Activity
在实现Activity.onBackPressed
时已经覆盖了它,然后为KeyEvent.KEYCODE_BACK
返回false
,这表明您尚未处理此事件,它应该继续传播。
To fix your problem stop overriding Activity.onKeyDown
.要解决您的问题,请停止覆盖
Activity.onKeyDown
。 Also, change your Activity.onBackPressed
to call super
if the SlidingDrawer
isn't opened.此外,如果未打开
SlidingDrawer
,请将Activity.onBackPressed
更改为调用super
。
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.