android 在TextField Jetpack Compose中第一次编译后键盘滞后

mtb9vblg  于 5个月前  发布在  Android
关注(0)|答案(1)|浏览(82)

我经历了第一次调试后的滞后,然后一切顺利。它发生在调试和发布(缩小)

AppInputText可组合如下:

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppInputText(
    modifier: Modifier = Modifier,
    valueProvider: () -> String,
    onValueChange: (String) -> Unit,
    labelText: String = "ilość",
    labelTextStyle: TextStyle = MaterialTheme.typography.labelMedium.copy(
        fontFamily = FontFamily(Font(R.font.open_sans_extrabold)),
    ),
    isSuffixEnabled: Boolean = false,
    inputTextStyle: TextStyle = MaterialTheme.typography.titleLarge.copy(
        lineHeight = 22.sp,
        fontWeight = FontWeight.Bold,
        color = Color(0xFF495A81).copy(alpha = 0.78f),
        textAlign = if (isSuffixEnabled) TextAlign.End else TextAlign.Start,
    ),
    registerQuantity: () -> Unit = {},
    isEnabled: Boolean = true,
    isReadOnly: Boolean = false,
    keyboardType: KeyboardType = KeyboardType.Number,
    amount: Int = 1,
    placeholderText: String = "Zeskanuj albo wpisz…",
) {
    val interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
    val singleLine = true
    val colors = OutlinedTextFieldDefaults.colors(
        focusedBorderColor = Color(0xFF667396),
        unfocusedBorderColor = Color(0xFF8892AC),
        focusedLabelColor = Color(0xFF8892AC),
        unfocusedLabelColor = Color(0xFF8892AC),
        focusedContainerColor = Color.White,
        unfocusedContainerColor = Color.White,
        errorContainerColor = Color.White,
        disabledContainerColor = Color.White,
        focusedTextColor = Color(0xFF495A81).copy(alpha = 0.78f),
        unfocusedTextColor = Color(0xFF495A81).copy(alpha = 0.78f),
        unfocusedTrailingIconColor = Color(0xFF495A81).copy(alpha = 0.6f),
        focusedTrailingIconColor = Color(0xFF495A81).copy(alpha = 0.6f),
    )
    val focusRequester = remember { FocusRequester() }
    val focusManager = LocalFocusManager.current
    val trailingIconText = @Composable {
        Icon(
            modifier = Modifier
                .clip(CircleShape)
                .clickable {
                    onValueChange("")
                    focusRequester.requestFocus()
                }
                .requiredSize(34.dp)
                .padding(5.dp)
                .background(
                    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f),
                    shape = CircleShape
                )
                .padding(4.dp),
            imageVector = Icons.Filled.Close,
            tint = MaterialTheme.colorScheme.surface.copy(alpha = 0.87f),
            contentDescription = "clear icon"
        )
    }

    BasicTextField(
        value = valueProvider(),
        onValueChange = onValueChange,
        modifier = modifier
            .defaultMinSize(minHeight = 52.dp)
            .focusRequester(focusRequester),
        interactionSource = interactionSource,
        enabled = isEnabled,
        readOnly = isReadOnly,
        singleLine = singleLine,
        textStyle = inputTextStyle,
        keyboardOptions = KeyboardOptions(
            imeAction = if (valueProvider().isNotBlank()) ImeAction.Done else ImeAction.Send,
            keyboardType = keyboardType,
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                defaultKeyboardAction(ImeAction.Done)
                focusManager.clearFocus()
                registerQuantity()
            }
        )
    ) { basicTextField ->
        OutlinedTextFieldDefaults.DecorationBox(
            isError = valueProvider().isBlank(),
            placeholder = {
                if (valueProvider().isBlank()) {
                    Text(
                        modifier = Modifier.padding(vertical = 6.dp),
                        text = placeholderText,
                        style = MaterialTheme.typography.labelMedium.copy(
                            fontSize = 16.sp,
                            fontFamily = FontFamily(Font(R.font.lato_light)),
                            color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.46f),
                        )
                    )
                }
            },
            value = valueProvider(),
            visualTransformation = VisualTransformation.None,
            innerTextField = basicTextField,
            label = {
                Text(
                    text = labelText.uppercase(),
                    style = labelTextStyle
                )
            },
            singleLine = singleLine,
            enabled = isEnabled,
            suffix = {
                if (isSuffixEnabled) {
                    Row {
                        Text(
                            text = " z $amount",
                            textAlign = TextAlign.Start,
                            style = MaterialTheme.typography.titleLarge.copy(
                                lineHeight = 22.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF495A81).copy(alpha = 0.78f),
                            ),
                        )
                        Spacer(modifier = Modifier.fillMaxWidth(0.8f))
                    }
                }
            },
            trailingIcon = if (valueProvider().isNotBlank() && !isReadOnly) trailingIconText else null,
            interactionSource = interactionSource,
            contentPadding = OutlinedTextFieldDefaults.contentPadding(
                top = 8.dp, bottom = 8.dp
            ),
            colors = colors,
            container = {
                OutlinedTextFieldDefaults.ContainerBox(
                    enabled = isEnabled,
                    isError = valueProvider().isBlank(),
                    colors = colors,
                    interactionSource = interactionSource,
                    shape = RoundedCornerShape(11.dp),
                    unfocusedBorderThickness = 1.33.dp,
                    focusedBorderThickness = 2.dp
                )
            },
        )
    }
}

字符串
通过valueProvider我传递文本状态,通过onValueChange状态通过事件,viewModels等进行更新。我试过使用derivedStateOf,但它没有解决这个问题。
我想问题可能是,在第一,当文本字段是空的,它有红色边框,它有文本下面的文本等,然后它的变化,这可能会导致更多的重组比需要,但我不能发现的问题。
另外,我只能打开键盘一次,如果我试着打开两次,它就不工作了。
100d1x

的字符串

6rqinv9w

6rqinv9w1#

你可能正在使用测试版.我遇到了这个问题在测试版的实现.同样的性能问题,就像你说:“另外,我可以打开键盘只有一次.如果我尝试打开它两次,它不工作."

implementation 'androidx.compose.material3:material3:1.2.0-beta01'.

字符串
最好坚持使用像这样的旧alpha版本,直到它被修复。

implementation 'androidx.compose.material3:material3:1.2.0-alpha11'

相关问题