【问题标题】:Jetpack Compose: ignoring descendants' content descriptionJetpack Compose:忽略后代的内容描述
【发布时间】:2022-06-10 18:41:06
【问题描述】:

假设我有一个带有行的列。每行都是一个逻辑单元,我希望 Talkback 逐行浏览列,而不选择行的后代。使用mergeDescendants = true 可以轻松实现这一目标

现在,我为 Row 定制了一个 contentDescription,它提供了更自然的描述。如何忽略要阅读的后代文本,以防止信息加倍?

tl;dr: 当使用mergeDescendants;如何让 Talkback 替换而不是合并后代的 contentDescriptions?

@Composable
fun Greeting() {
    // Goal: Each row is a single entity for talkback, that has a content desccription describing the entire tow
    // descendants should not be focused when navigating by swipe w/ talkback
    // "Greeting for Android X" should be read, descendants should not be read automatically.
    Column(modifier = Modifier.padding(20.dp)) {// Screen padding
        Box(
            // "greeting for Android 1 ... Hello Android! ... How do you do?" :-(
            // descendants can not be selected individually :-)
            Modifier.semantics(mergeDescendants = true) {
                contentDescription = "greeting for Android 1"
            }
        ) {
            Row {
                Text(text = "Hello Android!", modifier = Modifier.padding(32.dp))
                Text(text = "How do you do?", modifier = Modifier.padding(32.dp))
            }
        }
        Box(
            // "greeting for Android 2" :-)
            // descendants will be traversed next :-(
            Modifier.semantics {
                contentDescription = "greeting for Android 2"
            }
        ) {
            Row {
                Text(text = "Hello Android!", modifier = Modifier.padding(32.dp))
                Text(text = "How do you do?", modifier = Modifier.padding(32.dp))
            }
        }
        Box(
            // "greeting for Android 3" :-)
            // descendants can not be selected individually :-)

            // When using tap to speak rather than swipe, it is hard to select the row:
            // Only when tapping empty space will the row be selected.
            // When tapping either text element,nothing happens. :-(
            // Expected: the row is selected and "greeting for android 3" is read when 
            // I tap anywhere inside it's bounds
            Modifier.semantics {
                contentDescription = "greeting for Android 3"
            }
        ) {
            Row(Modifier.clearAndSetSemantics {}) {
                Text(text = "Hello Android!", modifier = Modifier.padding(32.dp))
                Text(text = "How do you do?", modifier = Modifier.padding(32.dp))
            }
        }
    }
}

【问题讨论】:

  • 嗨@nino-van-hooff,你成功了吗?我也有同样的问题...

标签: android accessibility android-jetpack-compose talkback


【解决方案1】:

我假设在您在顶级Box 上方提供的代码中代表您的“行”。尝试首先合并 Box 子项的语义属性,然后通过将以下修饰符应用于 Box 来清除所有子项并设置自定义内容描述:

Modifier
  .semantics(mergeDescendants = true) {}
  .clearAndSetSemantics { contentDescription = "greeting for Android 1" }

此外,您还可以在每个 Box 的子项上显式调用以下内容,以确保忽略它们的语义属性:

Modifier.clearAndSetSemantics {}

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 2011-10-04
    • 1970-01-01
    • 2019-10-28
    • 2022-11-03
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多