React Native Input组件采用ony数值

Ali*_*lov 7 validation numeric input-field react-native

在React-Native项目中,我有一个Native Base的Input组件,我希望这个组件只取数字值0-9并给出这个字段的默认值,我找了关于这个问题的其他问题,我不知道那些可能是那些答案是'textInput'组件或某种方式的建议对我不起作用?你能帮我吗?

小智 9

试试用

keyboardType="numeric"
Run Code Online (Sandbox Code Playgroud)

资源


Anu*_*mar 4

我明白你的问题,文本输入没有属性只能采用数字。但我有两种方法,在第一种方法中,你必须编写代码来获取数字值,这是黑客,但你可以使用它,代码是:

        <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)

对于第二种方法,请使用以下方法: