2016-11-16 33 views
2

在React-Native項目中,我有一個來自本機基地的輸入組件,我希望此組件只接受數字值0-9並給予此字段默認值,我尋找其他關於這個問題的問題,我不知道也許這些答案是'textInput'組件或以某種方式建議不適合我? 你能幫我嗎?React本機輸入組件需要ony數值

+0

參見:HTTPS ://stackoverflow.com/questions/32946793/react-native-textinput-that-only-accepts-numeric-characters – hippietrail

回答

1

我抓住你的問題沒有文本輸入的屬性只採取數字。但我有這個兩種方法,第一種方法,你必須編寫代碼,取值爲數字,這是黑客攻擊,但你可以使用它,代碼:

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

For second Method use this: