简体   繁体   English

React Native-无法在Android上的TextInput onChangeText回调中修改文本

[英]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.

相关问题 TextInput onChangeText不起作用React Native - TextInput onChangeText not working React Native TextInput React Native 无法在ChangeText 上工作 - TextInput React Native not working onChangeText 卡在 react native props - onSubmitEditing 和 onChangeText 中<textinput></textinput> - stuck in react native props - onSubmitEditing and onChangeText in <TextInput> 反应原生自定义TextInput onchangeText不起作用 - react native custom TextInput onchangeText Not working 反应本机,将TextInput设置onChangeText起作用 - react native, TextInput setting onChangeText to function 我们如何在响应本机中从onChangeText的标头的TextInput过滤数据? - How can we fliter data from header's TextInput on onChangeText in react native? 使用Redux将onChangeText中的TextInput格式化为React Native中的美元金额 - Format TextInput in onChangeText to dollar amount in React Native using Redux 如何将道具“OnChangeText”上的 TextInput 的“val”转换为 int。 反应本机 - How to convert to int the “val” of the TextInput on the prop “OnChangeText”. React Native React Native - TextInput 的 onChange 与 onChangeText 之间的区别 - React Native - Difference between onChange vs onChangeText of TextInput 如何在 textinput 内反应原生中使用两个 onChangeText - how to use two onChangeText in react native inside textinput
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM