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)}}
/>
答案 0 :(得分:7)
https://github.com/facebook/react-native/issues/6552#issuecomment-269989962
在Android 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)}}
/>
答案 1 :(得分:0)
每次更改后设置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}
/>