React Native:e.nativeEvent.key == 'Enter' 不起作用
React Native: e.nativeEvent.key == 'Enter' doesn't work
这个问题与 非常相似,但是,由于某些原因,除了 return 键(Enter 键)外,每个键都可以使用。如果密码正确,我想要的是让用户进入下一页。任何帮助将不胜感激
//代码
<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
}
}}
/>
仅当存在 multiline
TextInput
.
时,您才会获得 Enter 键的 onPress
事件
对于单行 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')
}
}}
/>
这个问题与
//代码
<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
}
}}
/>
仅当存在 multiline
TextInput
.
onPress
事件
对于单行 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')
}
}}
/>