作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在文本字段中,我想,当用户输入一个数字时,例如12345,它被格式化为 123.45。用户永远不需要输入小数位,它只使用最右边的 2 个数字作为小数位。该字段也应该只允许数字。这是针对 SwiftUI 项目的。提前感谢您的任何帮助。
最佳答案
由于您输入的内容和 TextField View 中显示的内容之间存在两种方式的绑定(bind),因此似乎无法对输入的显示数字进行插值。我建议一个小技巧:
.foregroundColor(.clear)
.keyboardType(.numberPad)
.accentColor(.clear)
隐藏光标specifier: "%.2f"
struct ContentView: View {
@State private var enteredNumber = ""
var enteredNumberFormatted: Double {
return (Double(enteredNumber) ?? 0) / 100
}
var body: some View {
Form {
Section {
ZStack(alignment: .leading) {
TextField("", text: $enteredNumber)
.keyboardType(.numberPad).foregroundColor(.clear)
.textFieldStyle(PlainTextFieldStyle())
.disableAutocorrection(true)
.accentColor(.clear)
Text("\(enteredNumberFormatted, specifier: "%.2f")")
}
}
}
}
}
关于swiftui - 将文本字段格式化为 2 位小数而不输入小数(SwiftUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58605993/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!