【问题标题】:How to auto focus a second text input after confirming the first in react native确认第一个反应本机后如何自动聚焦第二个文本输入
【发布时间】:2015-07-06 12:47:22
【问题描述】:

我有一个带有简单登录表单的 react 本机应用程序,其中包含两个文本输入:一个用于用户名,另一个用于密码。用户名 textInput 具有 autoFocus={'true'} 属性。提交后(虚拟键盘上的回车键),密码的第二个 textInput 应该自动对焦。有没有办法做到这一点?

【问题讨论】:

  • 按下第一个 textField 的 Return 按钮时使用 becomeFirstResponder

标签: ios react-native


【解决方案1】:

你需要在你的父组件上设置一个函数来关注密码字段:

function focusPassword() {
    this.refs.password.focus();
}

然后在第一个 TextInput 上定义 onSubmitEditing 属性。这将在用户按 Enter 键时调用(如果您使用 returnKeyType='next',则为“下一步”)

<TextInput ref='username' onSubmitEditing=this.focusPassword />
<TextInput ref='password' />

【讨论】:

    【解决方案2】:

    确保两个文本字段都有委托连接到您的 VC。 并为返回按钮使用 textfield 委托方法

    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    

    {

    if (textField == self.firstTextField)
    {
        [self.secondTextField becomeFirstResponder];
    }
    
    else [textField resignFirstResponder];
    
    return NO;
    

    }

    【讨论】:

    • 我认为 OP 正在寻找 react-native 代码,而不是 objc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多