react-native - react native : e. nativeEvent.key == 'Enter' 不起作用

标签 react-native textinput

这个问题与this非常相似,但是,出于某种原因,除了返回键(回车键)外,每个键都可以正常工作。如果密码正确,我想要的是让用户进入下一页。任何帮助将非常感激

//代码

        <TextInput
                style={styles.txtfield}
                placeholder="Password"
                placeholderTextColor = 'rgba(249, 129, 37, 1)'
                secureTextEntry={true}
                onChangeText={ password => this.setState({ password })}
                keyboardType="default"
                returnKeyType="next"                        
                onKeyPress={ (event) => {
                    if(event.nativeEvent.key == "Enter"){
                        alert(event.nativeEvent.key) // doesn't output anything nor execute the signin function
                        // this.signIn();
                    } 
                    else {
                        alert('Something else Pressed') // show a valid alert with the key info
                    }
                }}
            />

最佳答案

您将获得 onPress事件 输入 key 仅当存在 multilineTextInput .

单线用TextInput ,您将在 onSubmitEditing 中获得“Enter”或“Submit”按键事件方法。

  <TextInput
            style={styles.txtfield}
            placeholder="Password"
            placeholderTextColor = 'rgba(249, 129, 37, 1)'
            secureTextEntry={true}
            onChangeText={ password => this.setState({ password })}
            keyboardType="default"
            returnKeyType="next"
            onSubmitEditing={()=>{
                alert('on submit') // called only when multiline is false
            }}                        
            onKeyPress={ (event) => {
                if(event.nativeEvent.key == "Enter"){
                    alert(event.nativeEvent.key) //called when multiline is true
                    // this.signIn();
                } 
                else {
                    alert('Something else Pressed') 
                }
            }}
        />

关于react-native - react native : e. nativeEvent.key == 'Enter' 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508294/

相关文章:

unit-testing - React Native Expo 应用程序 : How to get it to run Jest tests

javascript - react native : View does not bound to the right

javascript - 输入时的默认文本

JSF inputText 和 inputSecret 大小不同

android - 聚焦TextInput时使用大写键盘

javascript - 如何在 firestore 中离线读取文档并写入(在线和离线)?

react-native - react native map mapPadding 不工作

react-native - CI/CD 管道 Azure Devops

ios - TextInpout 自动更正在 ios 中显示相反 [react native]

jquery - 如何在弹出窗口自动完成中输入文本