android - Onclick 被多次调用

标签 android android-layout onclick

我有一个 ListView ,其中包含一个复选框和可点击的textviews,定义如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusableInTouchMode="false"
        android:focusable="false">
        <CheckBox
            android:id="@+id/favouritebutton"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:text="Like"
            android:focusable="false"
            android:clickable="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:textSize="12sp">
        </CheckBox>
        <TextView
            android:id="@+id/behindPlanItemLabel"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:drawablePadding="-5dp"
            android:focusable="false"
            android:clickable="true"
            android:gravity="center_vertical"
            android:text="Plan"
            android:textColor="#000000"
            style="@style/behindPlanItemLabel"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/behindTimeItemLabel"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:drawablePadding="-5dp"
            android:gravity="center_vertical"
            android:text="Timer"
            android:focusable="false"
            android:clickable="true"
            android:textColor="#000000"
            style="@style/behindTimerItemLabel"
            android:textSize="12sp" />
    </LinearLayout>

我定义了一个CustomerAdapter,它扩展了CursorAdapter并实现了onClickListener。 在 newView 方法中,我在每个按钮和 TextView 上添加监听器,如下所示

        viewHolder.timerView.setOnClickListener(this);
        viewHolder.likeBox.setOnClickListener(this);
        viewHolder.planView.setOnClickListener(this);

我已经重写了 Onclick View 来捕捉点击。
当我单击布局中的第三项计时器时,
onclick 被调用一次,但是当我单击布局中的第二项“计划”时,
onCLick 被调用两次,当我单击“like”(布局中的第一项)时,onClick 被调用 3 次。
我只需要为任何点击获取 oneClick 回调。
对我在这里做错了什么有什么建议吗?

下面是完整的 onCLick 代码

      @Override
        public void onClick(View view) {
            int actionType;

            switch (view.getId()) {
                case R.id.favouritebutton:
                    Log.d(LOG_TAG, "favourite button");
                    actionType = Constants.CLICKED_FAV;
                    // Do something
                case R.id.behindPlanItemLabel:
                    Log.d(LOG_TAG, "plan button");
                    actionType = Constants.CLICKED_PLAN;
                    // Do something
                case R.id.behindTimeItemLabel:
                    Log.d(LOG_TAG, "timer button");
                    actionType = Constants.CLICKED_TIMER;
                    // Do something
            }
    }

最佳答案

感谢您的投入。我的问题现在通过在每个 switch case 语句中添加 return 得到解决。

           switch (view.getId()) {
                case R.id.favouritebutton:
                    Log.d(LOG_TAG, "favourite button");
                    actionType = Constants.CLICKED_FAV;
                    // Do something
                 return;
                case R.id.behindPlanItemLabel:
                    Log.d(LOG_TAG, "plan button");
                    actionType = Constants.CLICKED_PLAN;
                    // Do something
                   return;
                case R.id.behindTimeItemLabel:
                    Log.d(LOG_TAG, "timer button");
                    actionType = Constants.CLICKED_TIMER;
                    // Do something
                    return;
            }

关于android - Onclick 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37935474/

相关文章:

android - 不同Android SDK版本的不同主题

Android 底部工具栏像折断

android - 布局可能没用 - 不,它不是

javascript - jQuery 中的空输入框 onclick

Javascript onclick 更改变量值

java - 如何从 tflite 模型输出形状 [1, 28, 28,1] 的数组作为 android 中的图像

android - 根据屏幕密度获取drawable实际尺寸

android - 未编译的 PNG 文件作为参数传递。必须先编译成 .flat 文件

java - 检查从哪个线程调用方法

javascript - onclick() 事件 : random appearance of image