相关疑难解决方法(0)

React仅接受数字字符的Native TextInput

我需要一个React Native TextInput组件,只允许输入数字字符(0 - 9).我可以设置keyboardTypenumeric几乎让我在那里除了周期输入().但是,这无法阻止将非数字字符粘贴到字段中.

到目前为止我所提出的是使用该OnChangeText事件来查看输入的文本.我从文本中删除任何非数字字符.然后将文本放在状态字段中.然后更新TextInput它的Value属性.下面的代码片段.

<TextInput 
  style={styles.textInput}
  keyboardType = 'numeric'
  onChangeText = {(text)=> this.onChanged(text)}
  value = {this.state.myNumber}
/> 

onTextChanged(text) {
  // code to remove non-numeric characters from text
  this.setState({myNumber: text})
}
Run Code Online (Sandbox Code Playgroud)

这似乎工作,但它似乎是一个黑客.还有另一种方法吗?

react-native

68
推荐指数
11
解决办法
9万
查看次数

标签 统计

react-native ×1