我有一个组件,其中包含一个TextInput和一个TouchableHighlight并排。您点击文本框,键入您想要的内容,然后点击添加按钮以保存它。现在的问题是,键盘从键入时打开,您需要将其关闭,否则按钮将不会响应。如果我首先点击按钮,则键盘被解除,然后第二次点击才有效。我觉得我应该能够一次完成两个操作。这是我的渲染组件:
class FormInput extends Component {
constructor(props) {
super(props);
this.state = {
text: null
};
}
componentDidMount() {
this.refs.textInput.focus();
}
_changeTextEvent(event) {
this.setState({
text: event.nativeEvent.text
});
}
render() {
var style = [styles.textBox];
if (this.props.errors.length > 0) {
style.push(styles.errorTextBox);
}
var errors = null;
if (this.props.errors.length > 0) {
errors = this.props.errors.map((msg) => {
return (<Text style={styles.errorLabel}>{msg}</Text>);
});
}
return (
<View>
<View style={styles.container}>
<TextInput
ref='textInput'
style={style}
onChange={this._changeTextEvent.bind(this)}
autoFocus={true}
/>
<TouchableHighlight underlayColor="#96DBFF" style={styles.addButton} onPress={() => { this.props.pressEvent(this.state.text) }}>
<Text style={styles.addButtonText}>Add</Text>
</TouchableHighlight>
</View>
<View>{errors}</View>
</View>
);
}
}