Android SwitchCompat 禁用在文本点击时切换

7

我有一个 SwitchCompat

<android.support.v7.widget.SwitchCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some title"/>

我希望防止点击文本标题。

当我点击文本标题时,开关会切换。但是我只想在点击切换图标时进行切换。

这可能吗?还是我必须创建两个单独的视图:一个用于标题的 TextView,另一个没有文本的 SwitchCompat

enter image description here


你找到任何解决方案了吗? - Dalvinder Singh
1
我将它分成了两个视图。 - yital9
3个回答

0

希望不算太晚了...

由于我们有一些辅助功能限制,我无法创建一个单独的文本视图,但我想出了这个解决方案:

我只是创建了一个自定义组件并重写了onTouchEvent

override fun onTouchEvent(ev: MotionEvent): Boolean {
    if (ev.action == MotionEvent.ACTION_DOWN && thumbDrawable.bounds.contains(
            ev.x.toInt(),
            ev.y.toInt()
        )
    ) {
        toggle()
    }
    return true
}

0

我没有找到这个问题的确切解决方案,但是你可以像这样分开TextView和Swich:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YOUR SWITCH TEXT HERE"/>
    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
</RelativeLayout>

并在你的代码中使用 switch setOnCheckedChangeListener。


-4
请添加以下内容:

android:clickable="true"
android:focusable="true"

请在您的答案中添加更多描述! - Hamad
@Hamad 不过这也无关紧要。因为在使用 android 命名空间时,支持库的每几次迭代都会导致 InflateException 异常。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接