android - 单击 DrawerLayout 导致单击主布局
问题描述
我有带有 DrawerLayout 的 Activity,我实现了 Click ClickListener。问题是当 DrawerLayer 打开并且用户单击 darwerlayout 上的空白区域时,调用 OnClick 就好像他在主布局中单击了它后面的对象一样。我检查了 ID 是否出现在 onclick 中,它是抽屉后面的对象。
public class MainActivity extends AppCompatActivity implements
android.view.View.OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.resetThekir).setOnClickListener(this);
// when User Clicks on the drawer that's already opened and above the image
//it calls the Onclick as if he clicks on the Image.
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.addThkir:
case R.id.currentThkirTxt:
case R.id.thkirCount:
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/resetThkir"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="13dp"
android:layout_weight="1"
app:srcCompat="@drawable/reset"
android:contentDescription="@string/todo" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:entries="@array/menuitems"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#CCCCCC"/>
</android.support.v4.widget.DrawerLayout>
解决方案
您需要break
在每个案例之后添加:
public void onClick(View v) {
switch (v.getId()) {
case R.id.addThkir: addThkir() break;
case R.id.currentThkirTxt: currentThikTxt() break;
case R.id.thkirCount: thkirCount() break;
}
推荐阅读
- c++ - 从派生类初始化构造函数的问题
- javascript - 如何在调整大小时关闭滑入式导航栏?
- python - 即使我将变量设置为重新启动它,循环也不会重新启动
- git - 是否有命令将一棵 gitignore 树“卷起”到一个父 gitignore 中?
- kubernetes - 使用 helm 升级(部署 + PVC)到 Statefulset
- mysql - 对于按主键排序时的 Mysql ORDER BY LIMIT 查询,我需要复合索引吗?
- flutter - Fultter GridView 滚动
- mysql - 选择数据时至少过滤一个空记录
- docker - 如何访问历史泊坞窗'
' 图片 - c# - 如何在 EF Core 中的单独项目之间向 ApplicationUser 添加外键?