如何同步两个View的drawable状态

2024-02-22

在 Android 中,我有一个 EditText 和一个位于 EditText 旁边的按钮,每当我按下一个按钮时,我都希望另一个也以相同的状态出现。

我尝试将 android:clickable = "true" 放在封闭布局上,将 android:duplicateParentState="true" 放在我的 EditText 和 Button 上,但这仅在我触摸布局本身时才有效。如果我触摸我的 EditText 或 Button,什么也不会发生。我尝试在 EditText 和 Button 上设置 android:clickable = "false" 但触摸事件仍然不会过滤到父级。

如何使我的视图对触摸事件透明,以便它们传递给父级?我想要的是 EditText 和 Button 一起工作,这样如果我触摸其中任何一个,它们都会看起来被按下。

这是我当前使用的 XML:

<RelativeLayout
    android:id = "@+id/EnclosingLayout"
    android:layout_width = "fill_parent"
    android:layout_height = "120dp"
    android:clickable = "true">
    <EditText
        android:id = "@+id/MyEditText"                                                      
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:duplicateParentState="true"/>
    <Button
        android:id = "@+id/MyButton"
        android:layout_alignTop="@id/MyEditText"
        android:layout_alignBottom="@id/MyEditText"
        android:layout_alignParentRight="true"
        android:duplicateParentState="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="31.33dp"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:clickable="false"/>
</RelativeLayout>

我想你想要android:focusable="true"在你的RelativeLayout中而不是可点击的。我不确定你的意思是“我怎样才能使我的视图对触摸事件透明,以便它们传递给父级?”当您单击 EditText 时,您希望调用按钮 onClick() 吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何同步两个View的drawable状态 的相关文章

随机推荐