【问题标题】:Pass touches to child in xml在xml中将触摸传递给孩子
【发布时间】:2016-06-04 23:12:14
【问题描述】:

我做了一个自定义复选框xml如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checkbox_selected_glow" android:state_focused="true" android:state_pressed="true" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_selected_glow" android:state_focused="false" android:state_pressed="true" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_selected" android:state_focused="false" android:state_pressed="false" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_deselected_glow" android:state_focused="true" android:state_pressed="true" android:state_checked="false"/>
    <item android:drawable="@drawable/checkbox_deselected_glow" android:state_focused="false" android:state_pressed="true" android:state_checked="false"/>
    <item android:drawable="@drawable/checkbox_deselected" android:state_focused="false" android:state_pressed="false" android:state_checked="false"/>
</selector>

以及布局中的实现:

<CheckBox
      android:layout_width="30dp"
      android:layout_height="30dp"
      android:layout_gravity="center_vertical"
      android:paddingRight="10dp"
      android:button="@null"
      android:background="@drawable/custom_checkbox"/>

它工作得很好,并且对用户触摸它做出反应,当用户触摸它时,它会进入按下状态,而当用户释放触摸时,它会进入选择/取消选择状态。

此 CheckBox 与 TextView 中的某些文本相关联,并且两者都包含在 LinnearLayout 中。

我希望在用户按下 LinnearLayout 上的任何位置时激活 CheckBox 的按下状态。

有没有一个xml唯一的方法来做到这一点?

如果不是,我必须在 LinnearLayout 上重写哪些方法以编程方式使 CheckBox 对 LinnearLayout 任何部分的触摸做出反应?

【问题讨论】:

    标签: android xml checkbox android-view


    【解决方案1】:

    你可以试试这样的:

            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
            final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
            linearLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    checkBox.onTouchEvent(event);
                    return true;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      相关资源
      最近更新 更多