【问题标题】:React native: Which function is fired when There is a focus on TextInput and it reached its limit still a input button is pressedReact native:当焦点放在TextInput上并且达到其限制时触发哪个函数仍然按下输入按钮
【发布时间】:2019-12-17 06:24:49
【问题描述】:

我是 react-native 的新手。我有 3 个文本输入字段 t1, t2, t3。当达到极限时,我已经完成了对下一个输入字段的关注。现在我陷入了一种情况,不知何故,我的文本输入字段t1t1 已经达到了最大限制。但是当我按下另一个输入时,它并没有关注下一个输入字段t2

<TextInput
onChangeText={text => this.setState({ v1: text })}
keyboardType='number-pad'
maxLength={1}
autoCompleteType={'off'}
autoFocus={true}
ref={ref => this.v1Ref = ref}
onChange={event => {
if (event.nativeEvent.text !== '') {
  this.v2Ref.focus();
}
}}
/>

<TextInput
onChangeText={text => this.setState({ v2: text })}
keyboardType='number-pad'
maxLength={1}
autoCompleteType={'off'}
ref={ref => this.v2Ref = ref}
onChange={event => {
if (event.nativeEvent.text !== '') {
  this.v3Ref.focus();
}
}}
onKeyPress={({ nativeEvent }) => {
if (nativeEvent.key === 'Backspace') {
  if(this.state.v2== '')
    this.v1Ref.focus(); //here it is focusing on field t1 on backspace press and then if I am trying to give another input It's not going to t2 because t1 has reached its limit.
  }
}}
/>

【问题讨论】:

    标签: reactjs react-native react-native-textinput


    【解决方案1】:

    你的代码可以在我的电脑上运行

    如果您使用的是 Android 模拟器 这个函数不起作用,因为它是用the documentation写的

    注意:在 Android 上,仅处理来自软键盘的输入,而不是硬件键盘输入。

    如果您使用的是模拟器,则需要使用鼠标而不是计算机键盘来按下手机内部的按钮

    尝试插入 console.log 在函数里面看看它是否有效

    onKeyPress={({ nativeEvent }) => {
    console.log('onKeyPress')
    if (nativeEvent.key === 'Backspace') {
      if(this.state.v2== '')
        this.v1Ref.focus(); 
      }
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多