【问题标题】:onClick not work on LinearLayout with child SwitchonClick 不适用于带有子开关的 LinearLayout
【发布时间】:2020-01-15 10:29:24
【问题描述】:

我有一个带有 SwitchTextView 子小部件的自定义 LinearLayout。我希望能够点击LinearLayout覆盖的区域,所以我将clickable=true设置为LinearLayout,但是Switch没有被触发。

我想在点击LinearLayout 时触发Switch。 我还尝试在Switch 中使用android:duplicateParentState="true",但没有成功。

我可以通过XML 实现这一点吗?

 <LinearLayout
    android:background="@drawable/ripple"
    android:clickable="true"
    android:focusable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Switch
        android:clickable="false"
        android:paddingTop="8dp"
        android:text="Visibility"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:clickable="false"
        android:layout_marginRight="58dp"
        android:maxLines="3"
        android:text="@string/visibility_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="58dp" />

</LinearLayout>

【问题讨论】:

  • 你必须实现OnClickListener并在那里改变状态
  • @Md.Asaduzzaman 不,我想通过 xml 实现这一点,因为 Switch 切换没有任何监听器
  • 当你点击线性布局时你已经改变了 Switch 状态 aSwitch.setChecked(true);//
  • @ThavaSelvan 你的回答是适用的。
  • @ThavaSelvan 我想从XML实现

标签: android android-layout android-switch


【解决方案1】:

设置你的linearlayoutSwitchId,当你点击Linearlayout时你必须要检查Switch。

linearLayout.setOnClickListener
{
   onClick()
   {
      Switch.setChecked(true);}
   }
}

【讨论】:

  • @VijayKumar 您无法从 XML 实现它,因为您应该根据您的功能执行点击开关或线性布局。
【解决方案2】:

你可以试试下面的代码通过xml来实现——

<LinearLayout
android:background="@drawable/ripple"
android:clickable="true"
android:focusable="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Switch
    android:paddingTop="8dp"
    android:text="Visibility"
    android:textSize="16sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<TextView
    android:clickable="false"
    android:layout_marginRight="58dp"
    android:maxLines="3"
    android:text="@string/visibility_desc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="58dp" />

</LinearLayout>

【讨论】:

    【解决方案3】:

    您可以尝试以下代码以获得所需的输出

    <LinearLayout
        android:id="@+id/lnrLayout"
        android:background="@drawable/ripple"
        android:clickable="true"
        android:focusable="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <Switch
            android:id="@+id/switch"
            android:clickable="false"
            android:paddingTop="8dp"
            android:text="Visibility"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <TextView
            android:clickable="false"
            android:layout_marginRight="58dp"
            android:maxLines="3"
            android:text="@string/visibility_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="58dp" />
    
    </LinearLayout>
    

    然后设置linearlayout的setOnClickListener:

     lnrLayout.setOnClickListener{
                switch.performClick()
            }
    

    或者您可以使用带有文本的开关。

     <androidx.appcompat.widget.SwitchCompat
                    android:id="@+id/switch1"
                    style="@style/tvStyle_poppinsMedium_Small_Black"
                    android:text="@string/push_notifications"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    

    【讨论】:

    • 我想从XML实现
    • 我认为不可能从 XML 连接两个视图的点击事件。您还可以通过使用带有文本和所需样式的开关来实现所需的结果。
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多