关注 VStack 中的下一个 TextField [英] Focus on next TextField in VStack

查看:46
本文介绍了关注 VStack 中的下一个 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?

How can I change focus on text2 TextField, when I fill text1 and press "return"?

推荐答案

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

this should be possible with the code as below.

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

Now the textFields should be focusable and you would just need to handle the text input.

这篇关于关注 VStack 中的下一个 TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆