gpt4 book ai didi

android - Jetpack Compose 在 TextField 末尾设置光标

转载 作者:行者123 更新时间:2023-12-04 23:43:44 28 4
gpt4 key购买 nike

想为电话号码创建 TextField。
需要帮助在字段中设置光标始终在字符串末尾
我想做什么:
当在字段中获得新数字时,我得到所有已经输入的数字,而不是附加下划线(_)至少有 11 个字符串长度,然后返回带有数字的字符串或子字符串的“_”:return "+7(${template.substring(1, 4)}) ${template.substring(4,7)} ${template.substring(7,9)} ${template.substring(9, 11)}"如果光标总是在最后它可以工作,但如果不是数字顺序被破坏
完整代码:
@Composeble

@Composable
fun PhoneNumberEditText(
phoneNumState: MutableState<String>,
modifier: Modifier = Modifier,
imeAction: ImeAction = ImeAction.Done,
onImeAction: () -> Unit = {}
) {
TextField(
shape = RoundedCornerShape(16.dp),
value = phoneNumState.value,
onValueChange = { value ->
phoneNumState.value = value
val digits = phoneNumState.value.toCharArray().filter { it.isDigit() }
phoneNumState.value = phoneNumTemplate(digits)
},
modifier = modifier
.clip(RoundedCornerShape(16.dp))
.size(343.dp, 54.dp),
singleLine = true,
placeholder = {
Text(
"+7(___)_______", style = passwordTextStyle,
)
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = whiteBackground,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
cursorColor = greyColor,
textColor = greyColor
),
leadingIcon = {
Image(
painterResource(id = R.drawable.ic_phone),
stringResource(id = R.string.nomad_contentDescription_icon_phone)
)
},
keyboardOptions = KeyboardOptions(
imeAction = imeAction,
keyboardType = KeyboardType.Phone
),
)

}

换弦器:
fun phoneNumTemplate(chars: List<Char>): String {
return if (chars.isNotEmpty()) {
val digits = mutableListOf<Int>()
chars.forEach {
digits.add(it.toString().toInt())
}
val template = StringBuilder()
digits.forEach { template.append(it) }
for (i in 0..11) {
template.append("_")
}
"+7(${template.substring(1, 4)}) ${template.substring(4,7)} ${template.substring(7,9)} ${template.substring(9, 11)}"
} else {
"+7(___) ___ __ __"
}
}

谢谢您的帮助

最佳答案

这是不同的东西,但不是使用 onValueChange你可以试试 visualTransformation 属性(property)。
就像是:

TextField(
//your code...,
visualTransformation = PhoneNumberTransformation()

)
和:
class PhoneNumberTransformation() : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
return phoneNumFilter(text)
}
}

fun phoneNumFilter(text: AnnotatedString): TransformedText {

// +X(XXX)_XXX_XX_XX
val trimmed = if (text.text.length >= 11) text.text.substring(0..10) else text.text
var out = ""
for (i in trimmed.indices) {
if (i==0) out += "+"
if (i==1) out += "("
out += trimmed[i]
if (i==3) out +=") "
if (i==6 || i==8 ) out += " "

}

val phoneNumberOffsetTranslator = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
if (offset <= 0) return offset
if (offset <= 1) return offset +1
if (offset <= 3) return offset +2
if (offset <= 7) return offset +4
if (offset <= 9) return offset +5
if (offset <= 11) return offset +6
return 17

}

override fun transformedToOriginal(offset: Int): Int {
if (offset <=0) return offset
if (offset <=2) return offset -1
if (offset <=7) return offset -2
if (offset <=12) return offset -4
if (offset <=15) return offset -5
if (offset <=18) return offset -6
return 11
}
}

return TransformedText(AnnotatedString(out), phoneNumberOffsetTranslator)
}
enter image description here

关于android - Jetpack Compose 在 TextField 末尾设置光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67423074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com