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

最佳答案

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

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

https://stackoverflow.com/questions/63282660/

相关文章:

reactjs - EsLint 错误解释 no-unused-vars 中的导入语句

node.js - cors 问题与 passport.js google oauth 策略

caching - Mike Acton 的面向数据设计 - 'loops per cache li

python - 当某些列值为空时,如何合并 Dataframe 中的多行?

.net-core - 如何停止 blazor @onclick 重新渲染页面

angular - 如何更新 Angular 中提供者提供的值?

image - 在 vim 文件中插入 bash 脚本输出

c# - EpiServer DynamicDataStore LINQ 语句中的 sql 语法不正

azure - 如何在函数应用中为Azure SignalR服务使用不同的连接字符串?

php - 在 Laravel 中通过表格获得一行的最佳方法是什么?