【问题标题】:Capturing LinearLayout onClick events with ImageButton inside it使用其中的 ImageButton 捕获 LinearLayout onClick 事件
【发布时间】:2010-08-25 22:05:59
【问题描述】:

我有一个 ImageButton 和一个 TextView 包装在这样的 LinearLayout 中:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"></TextView>
    </LinearLayout>

ImageButton 由用于正常、聚焦和按下状态的自定义可绘制对象提供支持。我想允许用户单击 LinearLayout 中的任意位置来触发 OnClick 事件。下面的代码显示了 OnClickListener 的设置:

    final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide);
    actionHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(AppAdvocate.TAG, "Event caught");
        }
    });

当用户单击 TextView 上的任意位置但当用户单击 ImageButton 时该事件不会冒泡到 LinearLayout 时,该代码有效。我没有为按钮定义 onClickListener。我希望我的 ImageButton 的 drawable 发生变化,所以我不想将其设置为 clickable=false。有没有办法让活动冒泡?

【问题讨论】:

    标签: android android-linearlayout imagebutton onclicklistener


    【解决方案1】:

    为了使LinearLayout 可点击,您需要在其所有子元素上设置android:focusable="false"

    【讨论】:

    • 我设置了所有子“android:focusable=false”,但LinearLayout仍然无法点击
    【解决方案2】:

    如果你真的不想让按钮不可点击,你可以只为按钮添加一个监听器并执行与 LinearLayout onClick 相同的操作。在用户看来,它就像是一个大按钮。

    【讨论】:

    • 感谢 Mayra,这行得通。我应该如何将 LinearLayout onClick 事件级联到 ImageButton 以便在按下 LinearLayout 时其可绘制更改状态?
    • 我不确定您指的是哪个州。图像按钮是否像切换按钮一样显示点击状态?您应该可以在切换按钮上调用 setChecked(checkedState)。
    【解决方案3】:

    据我了解,您将需要以下内容:

        <LinearLayout android:orientation="vertical"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_weight="20" android:gravity="center"
            android:clickable="true" android:id="@+id/action_showhide">
            <ImageButton android:id="@+id/test"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/ic_toggle_hide_states" android:background="@null">         </ImageButton>
            <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/txtHide"
                android:textColor="@drawable/orange" android:textStyle="bold"
    android:onClick="viewClicked"></TextView>
        </LinearLayout>
    

    之后,您创建函数:

    public void viewClicked(View v){
    
        switch (v.getId())
        {
            case R.id.action_showhide:
                //do something
                break;
            case R.id.action_showhide:
                //do something
                break;
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      在 ImageButton 上设置 android::clickable="false"。这应该会有所帮助。

      【讨论】:

      • 不,请阅读问题:“所以我不想将其设置为 clickable=false”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多