You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Compose中点击父布局Row时仅在子RadioButton显示水波纹

实现点击整行仅RadioButton显示水波纹的方案

可行,具体实现思路是:剥离Row的selectable修饰符自带的水波纹,改用点击事件处理整行交互,同时保留无障碍属性,让RadioButton自身维护选中状态和水波纹效果。

代码示例

@Composable
fun RadioButtonWithText() {
    val selected = remember { mutableStateOf(false) }
    
    Row(
        modifier = Modifier
            .fillMaxWidth()
            // 处理整行点击,切换选中状态
            .pointerInput(Unit) {
                detectTapGestures {
                    selected.value = !selected.value
                }
            }
            // 维护无障碍访问属性,替代selectable的语义
            .semantics {
                this.selected = selected.value
                contentDescription = "自定义选项描述" // 根据实际场景修改
            }
            .padding(horizontal = 16.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        RadioButton(
            selected = selected.value,
            onClick = { selected.value = !selected.value },
            // RadioButton自带水波纹,无需额外设置
        )
        Text(
            text = "选项文本",
            modifier = Modifier.padding(start = 8.dp)
        )
    }
}

关键说明

  • 用pointerInput处理整行点击,替代selectable的点击逻辑,避免Row触发水波纹
  • 通过semantics修饰符补充无障碍属性,确保屏幕阅读器能正确识别选中状态和组件用途,不会破坏可访问性
  • RadioButton自身的onClick和selected绑定状态,点击整行或RadioButton本身都会切换状态,且只有RadioButton会显示自带的水波纹效果

内容的提问来源于stack exchange,提问作者Haf_SK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:47:06