android Jetpack编写基本文本字段无光标

kmynzznz  于 2023-04-04  发布在  Android
关注(0)|答案(2)|浏览(123)

请原谅,如果我问的东西真的很愚蠢,现在,但我是相当新的组成.我试图创建一个BasicTextField看起来像我想要的,但由于某种原因,没有光标 Flink ,当我选择了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)
                        )
                    }
                }
            }
        }
rkue9o1l

rkue9o1l1#

白色笔刷颜色可能是这里的罪魁祸首。
cursorBrush = SolidColor(Color.白色)

kknvjkwl

kknvjkwl2#

有两个问题:
1.光标颜色为白色:

cursorBrush = SolidColor(Color.Black),
  1. 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() // <--- 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)
                )
            }
        }
    }
}

相关问题