英文:
Jetpack compose basic textfield no cursor
问题
I'll provide the translated code portion without the comments:
原谅我如果我现在问了一个非常愚蠢的问题,但我对Compose还很新。我试图创建一个类似我想要的BasicTextField,但由于某种原因,当我选择了TextField并且键盘显示时,光标不会闪烁。我在普通的TextField中看到了它,但我不喜欢默认的外观。我是不是漏掉了某一行或某些内容?谢谢
fun CustomSearchField(state: MutableState<TextFieldValue>) {
val focusManager = LocalFocusManager.current
val keyboardController = LocalSoftwareKeyboardController.current
BasicTextField(
value = state.value,
onValueChange = { value ->
state.value = value
},
singleLine = true,
cursorBrush = SolidColor(Color.White),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
focusManager.clearFocus()
}
),
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
Text(
text = state.value.text,
style = poppinsRegular(14),
color = colorResource(id = R.color.primaryTextColor),
modifier = Modifier
.weight(1f)
)
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
}
}
英文:
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
fun CustomSearchField(state: MutableState<TextFieldValue>) {
val focusManager = LocalFocusManager.current
val keyboardController = LocalSoftwareKeyboardController.current
BasicTextField(
value = state.value,
onValueChange = { value ->
state.value = value
},
singleLine = true,
cursorBrush = SolidColor(Color.White),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
focusManager.clearFocus()
}
),
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
Text(
text = state.value.text,
style = poppinsRegular(14),
color = colorResource(id = R.color.primaryTextColor),
modifier = Modifier
.weight(1f)
)
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
答案1
得分: 1
白色刷子颜色可能是问题的原因。
cursorBrush = SolidColor(Color.White)
英文:
White brush color might be the culprit here.
cursorBrush = SolidColor(Color.White)
答案2
得分: 0
- 光标颜色是白色:
cursorBrush = SolidColor(Color.Black),
decorationBox
接受一个带有文本框和文本控件的参数。在decorationBox
函数中,你需要添加innerTextField ->
以指定该参数的名称。然后,在行内,你需要调用这个组合。
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
innerTextField() // <--- 在这里
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
英文:
There are two issues:
- The cursor color is white:
cursorBrush = SolidColor(Color.Black),
decorationBox
takes an argument with the text box and text controls. At thedecorationBox
function, you need to addinnerTextField ->
to make that the argument name. Then, inside the row, you need to call the composable.
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
innerTextField() // <--- HERE
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论