onContentSizeChange
似乎在Android上无法正确触发。事件在mount
上触发,但不在更改text height
触发。相同的代码适用于 iOS:
<TextInput
editable = {true}
multiline = {true}
[...]
onContentSizeChange={(event) => {
this.setState({height: event.nativeEvent.contentSize.height});
}}
style={{height: Math.max(35, this.state.height)}}
/>
https://github.com/facebook/react-native/issues/6552#issuecomment-269989962
在安卓中,onContentSizeChange
只被调用一次使用 onChange
而不是 onContentSizeChange
。
法典
<TextInput
editable = {true}
multiline = {true}
[...]
onChange={(event) => {
this.setState({height: event.nativeEvent.contentSize.height});
}}
style={{height: Math.max(35, this.state.height)}}
/>
每次
更改后设置value
都为我解决了问题
<TextInput
multiline={true}
onChangeText={(text) => {
this.setState({text});
}}
onContentSizeChange={(event) => {
this.setState({height: event.nativeEvent.contentSize.height});
}}
style={{height: this.state.height}}
value={this.state.text}
/>