text - 关注 VStack 中的下一个 TextField

标签 text swiftui textfield

我有这个代码:

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 的焦点?

最佳答案

这应该可以通过下面的代码实现。

@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 应该是可聚焦的,您只需要处理文本输入。

关于text - 关注 VStack 中的下一个 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63282660/

相关文章:

javascript - 在 onmousedown 的 Raphael 函数中包含 labelPath

SwiftUI 导航 View : how to update previous view every time coming back from the secondary view

java - 在java中聚焦时如何获取实际的文本字段

android - 根据 Material Design 文档创建文本字段框

algorithm - 分类文本时自动将类别相互链接

javascript - restFixture LET 用于文本/纯文本

ios - .ondelete 带有部分的 SwiftUI 列表

css - 搜索字段内的图像按钮?

java - 为什么我不能在 java ee 项目中使用 PDDOCUMENT PDFBOX ?

swift - 如何在 SwiftUI 中获得弹跳动画?