You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中发送按钮在键盘弹出时无法调用关联函数

解决React Native中Pressable在TextInput聚焦时首次点击不触发函数的问题

问题原因

在iOS(部分Android场景也会出现)中,当TextInput处于聚焦状态、键盘弹出时,点击非输入区域的Pressable,系统会优先触发TextInput的失焦操作,导致首次点击的onPress事件被拦截,只有第二次点击才会触发绑定的函数。

解决方案

这里提供两种实用的解决方法:

方法1:改用onPressIn替代onPress

onPressIn会在触摸按下的瞬间触发,不会等待系统处理失焦操作,能直接执行你的函数:

function msgInputHandler() {
    console.log("Test");
}

<View style={styles.msgInputContainer}>
  <TextInput
    placeholder="Message..."
    style={[styles.search, { width: "85%" }]}
  />
  <Pressable onPressIn={msgInputHandler}>        
    <Ionicons name="arrow-up-circle" size={40} color="orange" />       
  </Pressable>
</View>

方法2:手动管理TextInput焦点

通过ref手动控制TextInput失焦,确保函数能正常执行:

import { useRef } from 'react';

// ...

const inputRef = useRef(null);

function msgInputHandler() {
    // 先让输入框失焦
    inputRef.current?.blur();
    console.log("Test");
}

<View style={styles.msgInputContainer}>
  <TextInput
    ref={inputRef}
    placeholder="Message..."
    style={[styles.search, { width: "85%" }]}
  />
  <Pressable onPress={msgInputHandler}>        
    <Ionicons name="arrow-up-circle" size={40} color="orange" />       
  </Pressable>
</View>

补充说明

如果是Android端出现类似问题,还可以检查是否开启了android:focusableInTouchMode相关属性,确保Pressable的触摸事件能被优先捕获。

内容的提问来源于stack exchange,提问作者Suryasish Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:41:03