问题描述
对我来说,键盘应该自动打开而无需单击它,我使用了反应导航功能,因此从一个屏幕转到另一个屏幕时自动对焦无法正常工作
我尝试过:
import React,{Component} from 'react';
import {
View,StyleSheet,TextInput,TouchableOpacity,Keyboard,} from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.buildNameTextInput = React.createRef();
}
render() {
return (
<View style={styles.container}>
<View
style={{
backgroundColor: 'white',alignItems: 'center',flexDirection: 'row',width: '90%',paddingHorizontal: 10,}}>
<TouchableOpacity />
<TextInput
autoFocus={!!this.buildNameTextInput}
ref={this.buildNameTextInput}
style={{backgroundColor: 'white',width: '80%'}}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,justifyContent: 'center',backgroundColor: '#8ee3ab',},});
但是如果我使用它,进入下一屏后不起作用, 如果有任何解决方案请发布, 预先感谢...。
解决方法
使用引用集中文本输入this.refname.current.foicus()
检查以下代码
export default class App extends Component {
constructor(props) {
super(props);
this.input = React.createRef();
}
componentDidMount() {
this.input.current.focus()
}
render() {
return (
<View style={styles.container}>
<View
style={{
backgroundColor: 'white',alignItems: 'center',flexDirection: 'row',width: '90%',paddingHorizontal: 10,}}>
<TouchableOpacity />
<TextInput
ref={this.input}
style={{backgroundColor: 'white',width: '80%'}}
/>
</View>
</View>
);
}
}