37. Compose TextField 宽度自适应问题记录

宽度设置成width(IntrinsicSize.Min) 第一位数字被遮挡了一点点

  1. 这个问题好久了,被逼迫修复了,仔细观察是因为当光标插入的时候其价格会被挤压,把光标放置在最左侧就可了。

  2. 好好研究了一下TextStyle的api 其中就有一项被我看到了 TextIndent 这不就是缩进吗??? api解释:

     * Specify the indentation of a paragraph.
     *
     * @param firstLine the amount of indentation applied to the first line.
     * @param restLine the amount of indentation applied to every line except the first line.
     */
    
  3. 说一下 ,由于金额第一个字符被遮挡问题 是innerTextField代码内部的问题,使用Row()或者Box()套在外部去处理均不能解决问题。

  4. TextField控件在Compose 大版本升级的过程中 都有api的改动,说明还有很多地方在优化提升。有问题首先要用新的版本,再看问题。

  5. 庆祝Compose 1.3.0 (目前是beat版本) 已经可以drawText了,并且API很强大。

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PriceInput() {
    val focusRequester = FocusRequester()
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    val text = remember {
        mutableStateOf(TextFieldValue())
    }
    //这里可以改变 默认的颜色
    MaterialTheme(colors = lightColors(primary = Color.Magenta)) {
        BasicTextField(
            modifier = Modifier
                .width(IntrinsicSize.Min)
                .focusRequester(focusRequester),
            value =text.value,
            onValueChange = { str ->
                keyboardController?.show()
                text.value = str
            },
            singleLine = true,
            readOnly = false,
            cursorBrush = SolidColor(Color.Blue),
            textStyle = TextStyle(
                color = Color.Black,
                fontSize = 18.sp,
                textAlign = TextAlign.Start,
                textIndent = TextIndent(2.sp)
            ),
            decorationBox = { innerTextField ->
                if (text.value.text.isBlank())
                    Box(
                        modifier = Modifier.fillMaxHeight(),
                        contentAlignment = Alignment.CenterStart
                    ) {
                        Text(
                            "0",
                            modifier = Modifier.wrapContentWidth(),
                            style = TextStyle(color = Color.LightGray, fontSize = 18.sp, textIndent = TextIndent(2.sp)),
                        )
                        innerTextField()
                    }
                else innerTextField()
            },
            keyboardOptions = KeyboardOptions.Default.copy(
                autoCorrect = true,
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Done
            ),
            keyboardActions = KeyboardActions {
                keyboardController?.hide()
                focusManager.clearFocus(true)
            },
        )
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值