Jetpack Compose基本文本字段没有光标。

huangapple go评论102阅读模式
英文:

Jetpack compose basic textfield no cursor

问题

I'll provide the translated code portion without the comments:

  1. 原谅我如果我现在问了一个非常愚蠢的问题但我对Compose还很新我试图创建一个类似我想要的BasicTextField但由于某种原因当我选择了TextField并且键盘显示时光标不会闪烁我在普通的TextField中看到了它但我不喜欢默认的外观我是不是漏掉了某一行或某些内容谢谢
  2. fun CustomSearchField(state: MutableState<TextFieldValue>) {
  3. val focusManager = LocalFocusManager.current
  4. val keyboardController = LocalSoftwareKeyboardController.current
  5. BasicTextField(
  6. value = state.value,
  7. onValueChange = { value ->
  8. state.value = value
  9. },
  10. singleLine = true,
  11. cursorBrush = SolidColor(Color.White),
  12. keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
  13. keyboardActions = KeyboardActions(
  14. onDone = {
  15. keyboardController?.hide()
  16. focusManager.clearFocus()
  17. }
  18. ),
  19. decorationBox = {
  20. Row(
  21. horizontalArrangement = Arrangement.spacedBy(8.dp),
  22. verticalAlignment = Alignment.CenterVertically,
  23. modifier = Modifier
  24. .padding(horizontal = 16.dp)
  25. .fillMaxWidth()
  26. .heightIn(48.dp)
  27. .clip(shape = RoundedCornerShape(8.dp))
  28. .border(
  29. BorderStroke(
  30. 1.dp, colorResource(R.color.cardBorder)
  31. ), shape = RoundedCornerShape(8.dp)
  32. )
  33. ) {
  34. Image(
  35. painter = painterResource(id = R.drawable.ic_search_icon),
  36. contentDescription = null,
  37. contentScale = ContentScale.Crop,
  38. colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
  39. modifier = Modifier
  40. .padding(start = 16.dp)
  41. .size(24.dp)
  42. )
  43. if (state.value == TextFieldValue("")) {
  44. /*Text(
  45. text = "Search",
  46. style = poppinsRegular(14),
  47. color = colorResource(id = R.color.mainGray),
  48. modifier = Modifier
  49. .weight(1f)
  50. )*/
  51. } else {
  52. Text(
  53. text = state.value.text,
  54. style = poppinsRegular(14),
  55. color = colorResource(id = R.color.primaryTextColor),
  56. modifier = Modifier
  57. .weight(1f)
  58. )
  59. }
  60. if (state.value != TextFieldValue("")) {
  61. IconButton(
  62. onClick = {
  63. state.value =
  64. TextFieldValue("")
  65. }
  66. ) {
  67. Image(
  68. painter = painterResource(id = R.drawable.clear_icon),
  69. contentDescription = null,
  70. contentScale = ContentScale.Crop,
  71. colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
  72. modifier = Modifier
  73. .size(24.dp)
  74. .padding(2.dp)
  75. )
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
英文:

Forgive if I ask something really stupid now but I'm quite new to compose. I'm trying to create a BasicTextField which looks like I want it to but for some reason there is no cursor blinking when I've selected the TextField and the keyboard is showing. I saw it with a normal TextField but I didn't like the looks of the default things. Am I missing a line or something? Thanks

  1. fun CustomSearchField(state: MutableState&lt;TextFieldValue&gt;) {
  2. val focusManager = LocalFocusManager.current
  3. val keyboardController = LocalSoftwareKeyboardController.current
  4. BasicTextField(
  5. value = state.value,
  6. onValueChange = { value -&gt;
  7. state.value = value
  8. },
  9. singleLine = true,
  10. cursorBrush = SolidColor(Color.White),
  11. keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
  12. keyboardActions = KeyboardActions(
  13. onDone = {
  14. keyboardController?.hide()
  15. focusManager.clearFocus()
  16. }
  17. ),
  18. decorationBox = {
  19. Row(
  20. horizontalArrangement = Arrangement.spacedBy(8.dp),
  21. verticalAlignment = Alignment.CenterVertically,
  22. modifier = Modifier
  23. .padding(horizontal = 16.dp)
  24. .fillMaxWidth()
  25. .heightIn(48.dp)
  26. .clip(shape = RoundedCornerShape(8.dp))
  27. .border(
  28. BorderStroke(
  29. 1.dp, colorResource(R.color.cardBorder)
  30. ), shape = RoundedCornerShape(8.dp)
  31. )
  32. ) {
  33. Image(
  34. painter = painterResource(id = R.drawable.ic_search_icon),
  35. contentDescription = null,
  36. contentScale = ContentScale.Crop,
  37. colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue(&quot;&quot;)) R.color.mainGray else R.color.primaryTextColor)),
  38. modifier = Modifier
  39. .padding(start = 16.dp)
  40. .size(24.dp)
  41. )
  42. if (state.value == TextFieldValue(&quot;&quot;)) {
  43. /*Text(
  44. text = &quot;Search&quot;,
  45. style = poppinsRegular(14),
  46. color = colorResource(id = R.color.mainGray),
  47. modifier = Modifier
  48. .weight(1f)
  49. )*/
  50. } else {
  51. Text(
  52. text = state.value.text,
  53. style = poppinsRegular(14),
  54. color = colorResource(id = R.color.primaryTextColor),
  55. modifier = Modifier
  56. .weight(1f)
  57. )
  58. }
  59. if (state.value != TextFieldValue(&quot;&quot;)) {
  60. IconButton(
  61. onClick = {
  62. state.value =
  63. TextFieldValue(&quot;&quot;)
  64. }
  65. ) {
  66. Image(
  67. painter = painterResource(id = R.drawable.clear_icon),
  68. contentDescription = null,
  69. contentScale = ContentScale.Crop,
  70. colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
  71. modifier = Modifier
  72. .size(24.dp)
  73. .padding(2.dp)
  74. )
  75. }
  76. }
  77. }
  78. }

答案1

得分: 1

白色刷子颜色可能是问题的原因。

  1. cursorBrush = SolidColor(Color.White)
英文:

White brush color might be the culprit here.

  1. cursorBrush = SolidColor(Color.White)

答案2

得分: 0

  1. 光标颜色是白色:
  1. cursorBrush = SolidColor(Color.Black),
  1. decorationBox 接受一个带有文本框和文本控件的参数。在 decorationBox 函数中,你需要添加 innerTextField -> 以指定该参数的名称。然后,在行内,你需要调用这个组合。
  1. decorationBox = {
  2. Row(
  3. horizontalArrangement = Arrangement.spacedBy(8.dp),
  4. verticalAlignment = Alignment.CenterVertically,
  5. modifier = Modifier
  6. .padding(horizontal = 16.dp)
  7. .fillMaxWidth()
  8. .heightIn(48.dp)
  9. .clip(shape = RoundedCornerShape(8.dp))
  10. .border(
  11. BorderStroke(
  12. 1.dp, colorResource(R.color.cardBorder)
  13. ), shape = RoundedCornerShape(8.dp)
  14. )
  15. ) {
  16. Image(
  17. painter = painterResource(id = R.drawable.ic_search_icon),
  18. contentDescription = null,
  19. contentScale = ContentScale.Crop,
  20. colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
  21. modifier = Modifier
  22. .padding(start = 16.dp)
  23. .size(24.dp)
  24. )
  25. if (state.value == TextFieldValue("")) {
  26. /*Text(
  27. text = "Search",
  28. style = poppinsRegular(14),
  29. color = colorResource(id = R.color.mainGray),
  30. modifier = Modifier
  31. .weight(1f)
  32. )*/
  33. } else {
  34. innerTextField() // <--- 在这里
  35. }
  36. if (state.value != TextFieldValue("")) {
  37. IconButton(
  38. onClick = {
  39. state.value =
  40. TextFieldValue("")
  41. }
  42. ) {
  43. Image(
  44. painter = painterResource(id = R.drawable.clear_icon),
  45. contentDescription = null,
  46. contentScale = ContentScale.Crop,
  47. colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
  48. modifier = Modifier
  49. .size(24.dp)
  50. .padding(2.dp)
  51. )
  52. }
  53. }
  54. }
  55. }
英文:

There are two issues:

  1. The cursor color is white:
  1. cursorBrush = SolidColor(Color.Black),
  1. decorationBox takes an argument with the text box and text controls. At the decorationBox function, you need to add innerTextField -&gt; to make that the argument name. Then, inside the row, you need to call the composable.
  1. decorationBox = {
  2. Row(
  3. horizontalArrangement = Arrangement.spacedBy(8.dp),
  4. verticalAlignment = Alignment.CenterVertically,
  5. modifier = Modifier
  6. .padding(horizontal = 16.dp)
  7. .fillMaxWidth()
  8. .heightIn(48.dp)
  9. .clip(shape = RoundedCornerShape(8.dp))
  10. .border(
  11. BorderStroke(
  12. 1.dp, colorResource(R.color.cardBorder)
  13. ), shape = RoundedCornerShape(8.dp)
  14. )
  15. ) {
  16. Image(
  17. painter = painterResource(id = R.drawable.ic_search_icon),
  18. contentDescription = null,
  19. contentScale = ContentScale.Crop,
  20. colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue(&quot;&quot;)) R.color.mainGray else R.color.primaryTextColor)),
  21. modifier = Modifier
  22. .padding(start = 16.dp)
  23. .size(24.dp)
  24. )
  25. if (state.value == TextFieldValue(&quot;&quot;)) {
  26. /*Text(
  27. text = &quot;Search&quot;,
  28. style = poppinsRegular(14),
  29. color = colorResource(id = R.color.mainGray),
  30. modifier = Modifier
  31. .weight(1f)
  32. )*/
  33. } else {
  34. innerTextField() // &lt;--- HERE
  35. }
  36. if (state.value != TextFieldValue(&quot;&quot;)) {
  37. IconButton(
  38. onClick = {
  39. state.value =
  40. TextFieldValue(&quot;&quot;)
  41. }
  42. ) {
  43. Image(
  44. painter = painterResource(id = R.drawable.clear_icon),
  45. contentDescription = null,
  46. contentScale = ContentScale.Crop,
  47. colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
  48. modifier = Modifier
  49. .size(24.dp)
  50. .padding(2.dp)
  51. )
  52. }
  53. }
  54. }
  55. }
  56. </details>

huangapple
  • 本文由 发表于 2023年3月31日 04:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定