Однако я столкнулся со следующими проблемами:
Пример 1:
Я понял, что кликабельный модификатор внутренне создает фокусируемый узел, поэтому мой первый подход был:
Код: Выделить всё
@Composable
fun Sample1() {
val focusRequester = remember { FocusRequester() }
val interactionSource = remember { MutableInteractionSource() }
val isFocused = interactionSource.collectIsFocusedAsState().value
val color by remember(isFocused) { mutableStateOf(if (isFocused) Blue else Green) }
Column {
Box(
Modifier
.background(color)
.focusRequester(focusRequester)
.clickable(
interactionSource = interactionSource,
indication = null,
onClick = { focusRequester.requestFocus() }
)
) {
Text("Focusable box")
}
}
}
Пример 2:
Во второй попытке я вручную добавил фокусируемый узел, и теперь он получает фокус при щелчке. Однако появилась новая проблема: «двойной фокус» при навигации с помощью клавиши Tab, что имеет смысл, поскольку присутствуют и мой узел, и узел из кликабельного.
Код: Выделить всё
@Composable
fun Sample2() {
val focusRequester = remember { FocusRequester() }
val interactionSource = remember { MutableInteractionSource() }
val isFocused = interactionSource.collectIsFocusedAsState().value
val color by remember(isFocused) { mutableStateOf(if (isFocused) Blue else Green) }
Column {
Box(
Modifier
.background(color)
.focusRequester(focusRequester)
.focusable(interactionSource = interactionSource)
.clickable(
interactionSource = interactionSource,
indication = null,
onClick = { focusRequester.requestFocus() }
)
) {
Text("Focusable box")
}
}
}
Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/796 ... e-modifier
Мобильная версия