简体   繁体   中英

Jetpack compose accessibility with switch role

I'm trying to get a voiceover similar to XML:

  1. First the text is announced
  2. Then the state of the switch

The expected result is "Sometext on switch" or "Sometext off switch".

In Jetpack Compose it reads "Off sometext off switch" if you double-click it(using talkback to change state) and click again or swipe(to next and back), it reads "On sometext off switch".

Moreover, there is no sounding after double-clicking and changing state to on/off(like XML).

var isChecked by remember { mutableStateOf(false) }

val toggleModifier =
    Modifier.toggleable(
        value = isChecked,
        onValueChange = { isChecked = it },
        role = Role.Switch
    )

Box(modifier = Modifier.fillMaxSize()) {
    val rowModifier = toggleModifier
        .padding(16.dp)
        .align(Alignment.Center)
    Row(modifier = rowModifier) {
        Text(text = "Sometext")
        Switch(checked = isChecked, onCheckedChange = null)
    }
}

The second idea was to combine the text and the switch with mergeDescendants, but they are selected separately:

var isChecked by remember { mutableStateOf(false) }

val toggleModifier =
    Modifier.semantics(mergeDescendants = true) {}

Box(modifier = Modifier.fillMaxSize()) {
    val rowModifier = toggleModifier
        .padding(16.dp)
        .align(Alignment.Center)
    Row(modifier = rowModifier) {
        Text(text = "Sometext")
        Switch(checked = isChecked, onCheckedChange = { isChecked = !isChecked })
    }
}

As Utkarsh Tiwari mentioned in comments, it's compose bug, not fixed yet. They advice to use Role.Checkbox instead temporarily, it works as expected. Or do not use role at all

btw it's better to use Modifier.then(rowModifier) , then just modifier = rowModifier . So in the end it should be something like

Row(modifier = Modifier
.semantics(mergeDescendants = true) {}
.then(rowModifier)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM