With M3从1.1.0-alpha06
你可以使用suffix
属性:
TextField(
value = text,
onValueChange = { text = it },
suffix = { Text ("€") }
)
![enter image description here](https://i.stack.imgur.com/D5wyR.png)
Before M3 1.1.0-alpha06
或与M2或者你可以使用visualTransformation
属性。
就像是:
TextField(
value = text,
onValueChange = { text = it },
singleLine = true,
visualTransformation = SuffixTransformation(" €"),
)
class SuffixTransformation(val suffix: String) : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
val result = text + AnnotatedString(suffix)
val textWithSuffixMapping = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
return offset
}
override fun transformedToOriginal(offset: Int): Int {
if (text.isEmpty()) return 0
if (offset >= text.length) return text.length return offset
}
}
return TransformedText(result, textWithSuffixMapping )
}
}
![enter image description here](https://i.stack.imgur.com/yfFGh.gif)
如果您有占位符,您可以在其中添加一个条件visualTransformation
属性。
就像是:
TextField(
value = text,
onValueChange = { text = it },
singleLine = true,
visualTransformation = if (text.isEmpty())
VisualTransformation.None
else
SuffixTransformation(" €"),
placeholder = { Text("Placeholder") }
)