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

mtb9vblg  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(167)

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

AppInputText可组合如下:

  1. @OptIn(ExperimentalMaterial3Api::class)
  2. @Composable
  3. fun AppInputText(
  4. modifier: Modifier = Modifier,
  5. valueProvider: () -> String,
  6. onValueChange: (String) -> Unit,
  7. labelText: String = "ilość",
  8. labelTextStyle: TextStyle = MaterialTheme.typography.labelMedium.copy(
  9. fontFamily = FontFamily(Font(R.font.open_sans_extrabold)),
  10. ),
  11. isSuffixEnabled: Boolean = false,
  12. inputTextStyle: TextStyle = MaterialTheme.typography.titleLarge.copy(
  13. lineHeight = 22.sp,
  14. fontWeight = FontWeight.Bold,
  15. color = Color(0xFF495A81).copy(alpha = 0.78f),
  16. textAlign = if (isSuffixEnabled) TextAlign.End else TextAlign.Start,
  17. ),
  18. registerQuantity: () -> Unit = {},
  19. isEnabled: Boolean = true,
  20. isReadOnly: Boolean = false,
  21. keyboardType: KeyboardType = KeyboardType.Number,
  22. amount: Int = 1,
  23. placeholderText: String = "Zeskanuj albo wpisz…",
  24. ) {
  25. val interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
  26. val singleLine = true
  27. val colors = OutlinedTextFieldDefaults.colors(
  28. focusedBorderColor = Color(0xFF667396),
  29. unfocusedBorderColor = Color(0xFF8892AC),
  30. focusedLabelColor = Color(0xFF8892AC),
  31. unfocusedLabelColor = Color(0xFF8892AC),
  32. focusedContainerColor = Color.White,
  33. unfocusedContainerColor = Color.White,
  34. errorContainerColor = Color.White,
  35. disabledContainerColor = Color.White,
  36. focusedTextColor = Color(0xFF495A81).copy(alpha = 0.78f),
  37. unfocusedTextColor = Color(0xFF495A81).copy(alpha = 0.78f),
  38. unfocusedTrailingIconColor = Color(0xFF495A81).copy(alpha = 0.6f),
  39. focusedTrailingIconColor = Color(0xFF495A81).copy(alpha = 0.6f),
  40. )
  41. val focusRequester = remember { FocusRequester() }
  42. val focusManager = LocalFocusManager.current
  43. val trailingIconText = @Composable {
  44. Icon(
  45. modifier = Modifier
  46. .clip(CircleShape)
  47. .clickable {
  48. onValueChange("")
  49. focusRequester.requestFocus()
  50. }
  51. .requiredSize(34.dp)
  52. .padding(5.dp)
  53. .background(
  54. color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f),
  55. shape = CircleShape
  56. )
  57. .padding(4.dp),
  58. imageVector = Icons.Filled.Close,
  59. tint = MaterialTheme.colorScheme.surface.copy(alpha = 0.87f),
  60. contentDescription = "clear icon"
  61. )
  62. }
  63. BasicTextField(
  64. value = valueProvider(),
  65. onValueChange = onValueChange,
  66. modifier = modifier
  67. .defaultMinSize(minHeight = 52.dp)
  68. .focusRequester(focusRequester),
  69. interactionSource = interactionSource,
  70. enabled = isEnabled,
  71. readOnly = isReadOnly,
  72. singleLine = singleLine,
  73. textStyle = inputTextStyle,
  74. keyboardOptions = KeyboardOptions(
  75. imeAction = if (valueProvider().isNotBlank()) ImeAction.Done else ImeAction.Send,
  76. keyboardType = keyboardType,
  77. ),
  78. keyboardActions = KeyboardActions(
  79. onDone = {
  80. defaultKeyboardAction(ImeAction.Done)
  81. focusManager.clearFocus()
  82. registerQuantity()
  83. }
  84. )
  85. ) { basicTextField ->
  86. OutlinedTextFieldDefaults.DecorationBox(
  87. isError = valueProvider().isBlank(),
  88. placeholder = {
  89. if (valueProvider().isBlank()) {
  90. Text(
  91. modifier = Modifier.padding(vertical = 6.dp),
  92. text = placeholderText,
  93. style = MaterialTheme.typography.labelMedium.copy(
  94. fontSize = 16.sp,
  95. fontFamily = FontFamily(Font(R.font.lato_light)),
  96. color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.46f),
  97. )
  98. )
  99. }
  100. },
  101. value = valueProvider(),
  102. visualTransformation = VisualTransformation.None,
  103. innerTextField = basicTextField,
  104. label = {
  105. Text(
  106. text = labelText.uppercase(),
  107. style = labelTextStyle
  108. )
  109. },
  110. singleLine = singleLine,
  111. enabled = isEnabled,
  112. suffix = {
  113. if (isSuffixEnabled) {
  114. Row {
  115. Text(
  116. text = " z $amount",
  117. textAlign = TextAlign.Start,
  118. style = MaterialTheme.typography.titleLarge.copy(
  119. lineHeight = 22.sp,
  120. fontWeight = FontWeight.Bold,
  121. color = Color(0xFF495A81).copy(alpha = 0.78f),
  122. ),
  123. )
  124. Spacer(modifier = Modifier.fillMaxWidth(0.8f))
  125. }
  126. }
  127. },
  128. trailingIcon = if (valueProvider().isNotBlank() && !isReadOnly) trailingIconText else null,
  129. interactionSource = interactionSource,
  130. contentPadding = OutlinedTextFieldDefaults.contentPadding(
  131. top = 8.dp, bottom = 8.dp
  132. ),
  133. colors = colors,
  134. container = {
  135. OutlinedTextFieldDefaults.ContainerBox(
  136. enabled = isEnabled,
  137. isError = valueProvider().isBlank(),
  138. colors = colors,
  139. interactionSource = interactionSource,
  140. shape = RoundedCornerShape(11.dp),
  141. unfocusedBorderThickness = 1.33.dp,
  142. focusedBorderThickness = 2.dp
  143. )
  144. },
  145. )
  146. }
  147. }

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

的字符串

6rqinv9w

6rqinv9w1#

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

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

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

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

相关问题