[英]React Native - Can't modify text in TextInput onChangeText callback on Android
Because of a well-documented issue in React Native, autoCapitalize for TextInput doesn't work correctly on Android. 由于React Native中有一个记录良好的问题,因此TextCapation的autoCapitalize在Android上无法正常工作。 I tried to get around this by manually capitalizing the text in the onChangeText() callback: 我试图通过手动将onChangeText()回调中的文本大写来解决此问题:
onchgtext(newval)
{
this.s8 = newval.toUpperCase();
this.forceUpdate();
}
render()
{
...
<TextInput value={ this.s8 }
onChangeText={ (newval) => { this.onchgtext(newval) } }
...
/>
...
}
Unfortunately, modifying the text causes the TextInput to not work correctly: alternate keystrokes cause the entire previous text to be duplicated, but only if the keystroke was lowercase. 不幸的是,修改文本会导致TextInput无法正常工作: 交替的击键会导致重复整个先前的文本,但前提是击键是小写。
So, entering 1234
results in 1234
showing up; 因此,输入1234
会显示1234
。 entering ABCD
results in ABCD
showing up; 输入ABCD
会显示ABCD
; but entering abcd
results in AABCAABCD
. 但是输入abcd
导致AABCAABCD
。
Any ideas why this happens, and how to work around it? 有什么想法为什么会发生这种情况,以及如何解决呢? Note that using onChange
rather than onChangeText
results in the same behavior. 请注意,使用onChange
而不是onChangeText
导致相同的行为。
Could you try something like this, using state? 您可以使用state尝试类似的方法吗?
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = { text: 'Text' };
}
render() {
return (
<View>
<TextInput
onChangeText={(text) => {
this.setState({ text: text.toUpperCase() });
}}
value={this.state.text}
/>
</View>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.