撰写动态文本限制



我想在我的撰写应用程序中创建带有复选框的记事本它需要一个包含多个TextFields的LazyColumn。

要做到这一点,我需要以某种方式将应用程序中的单词限制为测量宽度

谁能帮我测量一下我可以限制多少字,根据视图宽度动态

@Composable
fun NoteField(
modifier: Modifier = Modifier,
textLimit: Int = 30
) {
val focusManager = LocalFocusManager.current

var text by remember { mutableStateOf(TextFieldValue("")) }
TextField(
modifier = modifier,
value = text,
onValueChange = {
if (it.text.length <= textLimit) text = it
else focusManager.moveFocus(FocusDirection.Down)
},
placeholder = { Text(stringResource(id = R.string.search)) },
colors = TextFieldDefaults.textFieldColors(
textColor = Theme.colors.Text,
disabledTextColor = Color.Transparent,
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent
),
maxLines = 1,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = {
focusManager.moveFocus(FocusDirection.Down)
}
)
)
}

您是否希望将复选框列表限制为一行,然后在文本溢出时使用a复选框移动到下一行?

如果你想这样做,你可以在文本填满屏幕宽度时使用overflow to ellipsis。但我不认为这是你想要的。

我认为你应该只是删除maxLines = 1,让文本域展开自己,这将是什么问题?

编辑!你也可以这样做使用onTextLayout,然后你可以检查溢出。出于演示目的,我将文本框和输入字段保存在Column中,然后可以通过将它们添加到Box(或任何浮动您的船)中来解决。

@Preview
@Composable
fun TextFieldWithOverflowDetection() {
val red = Color(0xFFe30613)
val white = Color(0xFFFFFFFF)
var value by remember { mutableStateOf("") }
var bgColor by remember { mutableStateOf(white) }
BasicTextField(
value = value,
onValueChange = { value = it },
modifier = Modifier
.background(white)
.fillMaxWidth(),
decorationBox = { innerTextField ->
Column { // change to e.g Box to overlap text and innerTextField
Text(
text = value,
maxLines = 1,
overflow = TextOverflow.Clip,
modifier = Modifier
.background(bgColor)
.fillMaxWidth(),
onTextLayout = { textLayoutResult ->
bgColor = if (textLayoutResult.hasVisualOverflow) red else white
},
)
innerTextField()
}
},
)
}

最新更新