我有此代码:
struct MyTestScreen: View {
@State var text1 = ""
@State var text2 = ""
var body: some View {
VStack {
TextField("text1", text: self.$text1)
TextField("text2", text: self.$text2)
}
}
}
当我填充text1并按“返回”时,如何更改对text2 TextField的关注?
答案 0 :(得分:0)
这应该可以通过下面的代码实现。
@State private var isFirstTextFieldFocused = false
@State private var isSecondTextFieldFocused = false
@State private var firstText: String = ""
@State private var secondText: String = ""
var body: some View {
VStack {
TextField("First text field", text: $firstText)
.padding()
.border(Color.white, width: isFirstTextFieldFocused ? 2 : 0)
.scaleEffect(isFirstTextFieldFocused ? 1.1 : 1)
.focusable(true) { focused in
isFirstTextFieldFocused = focused
}
TextField("Second text field", text: $secondText)
.padding()
.border(Color.white, width: isSecondTextFieldFocused ? 2 : 0)
.scaleEffect(isSecondTextFieldFocused ? 1.1 : 1)
.focusable(true) { focused in
isSecondTextFieldFocused = focused
}
}
}
现在 textFields 应该是可聚焦的,您只需要处理文本输入。