【问题标题】:React native onKeyPress is not working?反应原生 onKeyPress 不起作用?
【发布时间】:2018-01-02 17:08:32
【问题描述】:

我刚刚放置了 TextInput KeyboardType ={"numeric"} 只是试图找到天气用户输入'-' 如果意味着我必须首先在 OnChange 中输入 event.preventDefault()。后来尝试了 onKeyPress 但 onKeyPress 没有触发,我只是试图这样做但它允许我输入显示警告合成事件性能问题。

各位帮帮我。

【问题讨论】:

  • 您在哪个平台上测试?
  • 你在用安卓模拟器测试吗?
  • Andriod no in mobile

标签: react-native


【解决方案1】:
 const App =()=>{
  const  handleKeyPress = ({ nativeEvent: { key: keyValue } }) => {
      console.log(keyValue);
      if(keyValue === 'Enter')
      {
        console.log("enter");
      }
  };
  return(
    <View>
         <TextInput
            placeholder="Your Password"
            placeholderTextColor="#666666"
            style={styles.inputSearch}
            // onEndEditing={(e)=>handleValidUser(e.nativeEvent.text)}
            onKeyPress={handleKeyPress}
          />
    </View>
  )
}

【讨论】:

    【解决方案2】:

    这里是合成事件这个问题的解决方案:

    输入元素:

    <Input onKeyPress = {this.keyPress} />
    

    按键功能

    keyPress = (event) => {
    
    /* this line will solve your error */
    
    event.persist();
    console.log(event);
    }
    
    

    【讨论】:

      【解决方案3】:

      您正在使用 android mobile 进行测试,而 onKeyPress 方法适用于 iOS,请检查 documentation 是否相同

      您可以使用onChangeText 进行文本更改。

      例子:

      <TextInput
          onChangeText={(text) => this.onFirstNameText(text)}
          returnKeyType={'next'}
          underlineColorAndroid='transparent'
          style={styles.inputColLeft}
      />
      

      编辑 2018

      根据this commit,Android 设备现在也支持onKeyPress

      来自文档:

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

      【讨论】:

      • 还没用,但你需要吗?,验证数字只有你可以检查:stackoverflow.com/questions/32946793/…
      • onKeyPress 现在支持 Android。
      • 对于Android,它只支持默认键盘,对于'numeric'等非默认键盘,它不起作用
      【解决方案4】:

      像这样使用 onChangeText:

      <TextInput  onChangeText = {(text) => this.getPhone(text)} keyboardType="numeric" autoCapitalize="none" autoCorrect={false} onSubmitEditing={ () => this.addressInput.focus()} style={styles.input} placeholder="phone" placeholderTextColor="rgba(255,255,255,0.6)" underlineColorAndroid="rgba(0,0,0,0.0)" returnKeyType="next"/>
      
       getPhone = (text) => {
            this.setState({phone: text})
            //use your logic here 
      
         }
      

      【讨论】:

        猜你喜欢
        • 2019-09-13
        • 1970-01-01
        • 2021-06-23
        • 2021-03-12
        • 2021-01-26
        • 2017-02-16
        • 1970-01-01
        • 2021-11-10
        • 2021-10-22
        相关资源
        最近更新 更多