15

我有一个自定义的可组合视图(本质上是 Surface + Text),我想根据焦点状态更改表面的颜色。FocusManager#FocusNode 被标记为内部的,我不知道有什么方法可以实现这一点。这只是尚不可用吗?还有其他人必须解决这个问题吗?

4

4 回答 4

21

1.0.x您可以使用来Modifier.onFocusChanged观察焦点状态事件。
就像是:

var color by remember { mutableStateOf(Black) }
val focusRequester = FocusRequester()

Text(
    modifier = Modifier
        .focusRequester(focusRequester)
        .onFocusChanged { color = if (it.isFocused) Green else Black }
        .focusModifier()
        .pointerInput(Unit) { detectTapGestures { focusRequester.requestFocus() } },
    text = "Text",
    color = color
)
于 2020-09-02T15:04:51.347 回答
11

截至dev11,FocusManagerAmbient已被弃用而赞成FocusModifier. 有关更多示例,请查看KeyInputDemoComposeInputFieldFocusTransitionFocusableDemo

@Composable
private fun FocusableText(text: MutableState<String>) {
    val focusModifier = FocusModifier()
    Text(
        modifier = focusModifier
            .tapGestureFilter { focusModifier.requestFocus() }
            .keyInputFilter { it.value?.let { text.value += it; true } ?: false },
        text = text.value,
        color = when (focusModifier.focusState) {
            Focused -> Color.Green
            NotFocused -> Color.Black
            NotFocusable -> Color.Gray
        }
    )
}

此后 Compose 已更新并制作了FocusManager成员public;虽然,我不确定 api 的最终结果如何dev10。但截至目前,您可以创建一个FocusNode并使用FocusManager.registerObserver.

val focusNode = remember {
    FocusNode().apply {
        focusManager.registerObserver(node = this) { fromNode, toNode ->
            if (toNode == this) {
                // has focus
            } else {
                // lost focus
            }
        }
    }
}

如果您想获得焦点,您现在可以致电FocusManager.requestFocus

val focusManager = FocusManagerAmbient.current
focusManager.requestFocus(focusNode)

您还可以focusIdentifier在您的FocusNode:

val focusNode = remember {
    FocusNode().apply {
        ...
        focusManager.registerFocusNode("your-focus-identifier", node = this)
    }
}

要获得特定标识符的焦点,您只需调用FocusManager.requestFocusById

使用它,您可以轻松地创建一个Composable可以为您提供和请求焦点的方法,例如:

@Composable
fun useFocus(focusIdentifier: String? = null): Pair<Boolean, () -> Unit> {
    val focusManager = FocusManagerAmbient.current
    val (hasFocus, setHasFocus) = state { false }
    val focusNode = remember {
        FocusNode().apply {
            focusManager.registerObserver(node = this) { fromNode, toNode ->
                setHasFocus(toNode == this)
            }
            focusIdentifier?.let { identifier ->
                focusManager.registerFocusNode(identifier, node = this)
            }
        }
    }
    onDispose {
        focusIdentifier?.let { identifier ->
            focusManager.unregisterFocusNode(identifier)
        }
    }
    return hasFocus to {
        focusManager.requestFocus(focusNode)
    }
}
val (hasFocus, requestFocus) = useFocus("your-focus-identifier")

你也可以和它一起组成孩子:

@Composable
fun FocusableTextButton(
    text: String,
    focusedColor: Color = Color.Unset,
    unFocusedColor: Color = Color.Unset,
    textColor: Color = Color.White,
    focusIdentifier: String? = null
) {
    val (hasFocus, requestFocus) = useFocus(focusIdentifier)
    Surface(color = if (hasFocus) focusedColor else unFocusedColor) {
        TextButton(onClick = requestFocus) {
            Text(text = text, color = textColor)
        }
    }
}

或者,还有FocusModifier,截至目前是:

/**
 * A [Modifier.Element] that wraps makes the modifiers on the right into a Focusable. Use a
 * different instance of [FocusModifier] for each focusable component.
 *
 * TODO(b/152528891): Write tests for [FocusModifier] after we finalize on the api (API
 * review tracked by b/152529882).
 */

但我认为您现在不能使用它应用标识符。

val focusModifier = FocusModifier()
val hasFocus = focusModifier.focusDetailedState == FocusDetailedState.Active
Surface(
    modifier = focusModifier,
    color = if (hasFocus) focusedColor else unFocusedColor
) {
    TextButton(onClick = { focusModifier.requestFocus() }) {
        Text(text = text, color = textColor)
    }
}

话虽如此,我不能 100% 确定这是现在处理焦点的预期方式。我大量引用了CoreTextField以了解它是如何处理的。

例子:

@Composable
fun FocusTest() {
    val focusManager = FocusManagerAmbient.current
    val selectRandomIdentifier: () -> Unit = {
        focusManager.requestFocusById(arrayOf("red,", "blue", "green", "yellow").random())
    }
    Column(verticalArrangement = Arrangement.SpaceBetween) {
        FocusableTextButton(
            text = "When I gain focus, I turn red",
            focusedColor = Color.Red,
            focusIdentifier = "red"
        )
        FocusableTextButton(
            text = "When I gain focus, I turn blue",
            focusedColor = Color.Blue,
            focusIdentifier = "blue"
        )
        FocusableTextButton(
            text = "When I gain focus, I turn green",
            focusedColor = Color.Green,
            focusIdentifier = "green"
        )
        FocusableTextButton(
            text = "When I gain focus, I turn yellow",
            focusedColor = Color.Yellow,
            focusIdentifier = "yellow"
        )
        Button(onClick = selectRandomIdentifier) {
            Text(text = "Click me to randomly select a node to focus")
        }
    }
}

gif

于 2020-05-09T01:29:35.753 回答
3

这段代码对我有用

var haveFocus by remember { mutableStateOf(false) }
Surface(
    modifier = Modifier.onFocusEvent {
        haveFocus = it.isFocused
    },
    shape = RectangleShape
){...}
于 2021-03-19T01:29:39.557 回答
0

这个答案基于 Gabriele Mariotti 的答案,但我有改变

  • 用于remember { FocusRequester() } 防止FocusRequester is not initialized第二次点击后崩溃Text
  • 使用focusTarget而不是focusModifier因为已弃用

.

var color by remember { mutableStateOf(Color.Black) }
val focusRequester = remember { FocusRequester() }

Text(
    text = "Hello",
    modifier = Modifier
        .focusRequester(focusRequester)
        .onFocusChanged { color = if (it.isFocused) Color.Green else Color.Black }
        .focusTarget()
        .pointerInput(Unit) { detectTapGestures { focusRequester.requestFocus() } }
)
于 2022-01-12T03:11:06.080 回答