跳至主内容
版本:0.83

TouchableWithoutFeedback

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

技巧

如果您正在寻找更全面且面向未来的触摸输入处理方案,请参阅 Pressable API。

除非有充分理由,否则不要使用此组件。所有响应按压的元素在触摸时都应提供视觉反馈。

TouchableWithoutFeedback 仅支持单个子元素。如需包含多个子组件,请将它们包裹在 View 组件中。重要的是,TouchableWithoutFeedback 通过克隆子元素并应用响应器属性(responder props)来实现功能。因此要求任何中间组件都必须将这些属性传递到底层的 React Native 组件。

使用模式

tsx
function MyComponent(props: MyComponentProps) {
return (
<View {...props} style={{flex: 1, backgroundColor: '#fff'}}>
<Text>My Component</Text>
</View>
);
}

<TouchableWithoutFeedback onPress={() => alert('Pressed!')}>
<MyComponent />
</TouchableWithoutFeedback>;

示例


id: touchablenativefeedback title: 触摸原生反馈

参考

属性

accessibilityIgnoresInvertColors
iOS

该值决定在开启颜色反转时,此视图是否应被反转。设置为 true 表示即使开启颜色反转,视图也不会被反转。

更多信息请参阅无障碍功能指南

Type
Boolean

id: touchablenativefeedback title: 触摸原生反馈

accessible

当值为 true 时,表示该视图是无障碍元素。默认情况下所有可触摸元素均为无障碍元素。

Type
bool

id: touchablenativefeedback title: 触摸原生反馈

accessibilityLabel

覆盖屏幕阅读器在用户与元素交互时读取的文本内容。默认情况下,标签会通过遍历所有子元素并拼接所有 Text 节点的文本(以空格分隔)来生成。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

accessibilityLanguage
iOS

该值指示屏幕阅读器在用户与元素交互时应使用的语言,必须遵循 BCP 47 规范

更多信息请参阅 iOS accessibilityLanguage 文档

Type
string

id: touchablenativefeedback title: 触摸原生反馈

accessibilityHint

当无障碍标签无法明确操作结果时,无障碍提示可帮助用户理解在无障碍元素上执行操作将产生的效果。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

accessibilityRole

accessibilityRole 向辅助技术用户传达组件的用途。

accessibilityRole 可选值包括:

  • 'none' - 元素无特定角色时使用

  • 'button' - 元素应作为按钮处理时使用

  • 'link' - 元素应作为链接处理时使用

  • 'search' - 文本字段元素需同时作为搜索框处理时使用

  • 'image' - 元素应作为图像处理时使用(例如可与按钮或链接组合)

  • 'keyboardkey' - 元素充当键盘按键时使用

  • 'text' - 元素应作为不可更改的静态文本处理时使用

  • 'adjustable' - 元素支持数值调整时使用(如滑块)

  • 'imagebutton' - 元素同时作为按钮和图像处理时使用

  • 'header' - 元素作为内容区块标题时使用(例如导航栏标题)

  • 'summary' - 当元素可用于在应用首次启动时提供当前状态的快速摘要时使用。

  • 'alert' - 当元素包含需要向用户展示的重要文本时使用。

  • 'checkbox' - 当元素代表可被选中、未选中或混合选中状态的复选框时使用。

  • 'combobox' - 当元素代表组合框(允许用户从多个选项中选择)时使用。

  • 'menu' - 当组件是选择菜单时使用。

  • 'menubar' - 当组件是多个菜单的容器时使用。

  • 'menuitem' - 用于表示菜单内的单个选项。

  • 'progressbar' - 用于表示任务进度的组件。

  • 'radio' - 用于表示单选按钮。

  • 'radiogroup' - 用于表示一组单选按钮。

  • 'scrollbar' - 用于表示滚动条。

  • 'spinbutton' - 用于表示可展开选项列表的按钮。

  • 'switch' - 用于表示可开关的切换按钮。

  • 'tab' - 用于表示标签页。

  • 'tablist' - 用于表示标签页列表。

  • 'timer' - 用于表示计时器。

  • 'toolbar' - 用于表示工具栏(操作按钮或组件的容器)。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

accessibilityState

向辅助技术用户描述组件的当前状态。

更多信息请参阅无障碍功能指南

Type
object: {disabled: bool, selected: bool, checked: bool or 'mixed', busy: bool, expanded: bool}

id: touchablenativefeedback title: 触摸原生反馈

accessibilityActions

辅助操作允许辅助技术以编程方式触发组件的操作。accessibilityActions 属性应包含操作对象列表,每个对象需包含 name 和 label 字段。

详见辅助功能指南

Type
array

id: touchablenativefeedback title: 触摸原生反馈

aria-busy

表示元素正在更新中,辅助技术可能需要等待变更完成后再通知用户。

TypeDefault
booleanfalse

id: touchablenativefeedback title: 触摸原生反馈

aria-checked

表示可勾选元素的状态。该字段可接受布尔值或 "mixed" 字符串(表示混合状态的复选框)。

TypeDefault
boolean, 'mixed'false

id: touchablenativefeedback title: 触摸原生反馈

aria-disabled

表示元素可见但处于禁用状态,因此不可编辑或操作。

TypeDefault
booleanfalse

id: touchablenativefeedback title: 触摸原生反馈

aria-expanded

指示可展开元素当前是展开还是折叠状态。

TypeDefault
booleanfalse

id: touchablenativefeedback title: 触摸原生反馈

aria-hidden

表示该元素是否对辅助技术隐藏。

例如,在包含同级视图 AB 的窗口中,将视图 Baria-hidden 设为 true 会导致 VoiceOver 忽略 B 元素及其子元素。

TypeDefault
booleanfalse

id: touchablenativefeedback title: 触摸原生反馈

aria-label

定义用于标记交互元素的字符串值。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

aria-live
Android

表示元素将被更新,并描述用户代理、辅助技术和用户可以从实时区域中预期的更新类型。

  • off 辅助服务不应播报此视图的变更

  • polite 辅助服务应播报此视图的变更

  • assertive 无障碍服务应中断当前语音播报,立即宣布该视图的变更。

TypeDefault
enum('assertive', 'off', 'polite')'off'

id: touchablenativefeedback title: 触摸原生反馈

aria-modal
iOS

布尔值,指示 VoiceOver 是否应忽略接收器同级视图内的元素。优先级高于 accessibilityViewIsModal 属性。

TypeDefault
booleanfalse

id: touchablenativefeedback title: 触摸原生反馈

aria-selected

指示可选元素当前是否被选中。

Type
boolean

onAccessibilityAction

当用户执行辅助操作时触发,该函数接收的唯一参数是包含要执行操作名称的事件对象。

详见辅助功能指南

Type
function

id: touchablenativefeedback title: 触摸原生反馈

accessibilityValue

表示组件的当前值。可以是组件值的文本描述,对于基于范围的组件(如滑块和进度条),则包含范围信息(最小值、当前值和最大值)

更多信息请参阅辅助功能指南

Type
object: {min: number, max: number, now: number, text: string}

id: touchablenativefeedback title: 触摸原生反馈

aria-valuemax

表示基于范围的组件(如滑块和进度条)的最大值。优先级高于 accessibilityValue 属性中的 max 值。

Type
number

id: touchablenativefeedback title: 触摸原生反馈

aria-valuemin

表示基于范围的组件(如滑块和进度条)的最小值。优先级高于 accessibilityValue 属性中的 min 值。

Type
number

id: touchablenativefeedback title: 触摸原生反馈

aria-valuenow

表示基于范围的组件(如滑块和进度条)的当前值。优先级高于 accessibilityValue 属性中的 now 值。

Type
number

id: touchablenativefeedback title: 触摸原生反馈

aria-valuetext

表示组件的文本描述。优先级高于 accessibilityValue 属性中的 text 值。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

delayLongPress

onPressIn 到触发 onLongPress 的时长(毫秒)

Type
number

id: touchablenativefeedback title: 触摸原生反馈

delayPressIn

从触摸开始到触发 onPressIn 的持续时间(毫秒)。

Type
number

id: touchablenativefeedback title: 触摸原生反馈

delayPressOut

从触摸释放到触发 onPressOut 的持续时间(毫秒)。

Type
number

id: touchablenativefeedback title: 触摸原生反馈

disabled

若为 true,则禁用此组件的所有交互。

Type
bool

id: touchablenativefeedback title: 触摸原生反馈

hitSlop

定义触摸可以从按钮外多远开始触发。当手指移出按钮区域时,此值会累加到 pressRetentionOffset 上。

备注

触摸区域永远不会超出父视图边界,且当触摸点落在两个重叠视图上时,兄弟视图的 Z-index 始终具有优先级。

Type
Rect or number

id

用于在原生代码中定位此视图。优先级高于 nativeID 属性。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

onBlur

当元素失去焦点时调用。

Type
({nativeEvent: TargetEvent}) => void

id: touchablenativefeedback title: 触摸原生反馈

onFocus

当元素获得焦点时调用。

Type
({nativeEvent: TargetEvent}) => void

id: touchablenativefeedback title: 触摸原生反馈

onLayout

在挂载时和布局变化时触发。

Type
({nativeEvent: LayoutEvent}) => void

id: touchablenativefeedback title: 触摸原生反馈

onLongPress

onPressIn 后的持续时间超过 370 毫秒时触发。可通过 delayLongPress 自定义此时间阈值。

Type
function

id: touchablenativefeedback title: 触摸原生反馈

onPress

当触摸释放时调用(若被取消则不触发,例如滚动操作抢占了响应者锁)。首个函数参数为 PressEvent 类型事件。

Type
function

id: touchablenativefeedback title: 触摸原生反馈

onPressIn

在可触摸元素被按下时立即调用(甚至先于 onPress)。适用于发起网络请求等场景。首个函数参数为 PressEvent 类型事件。

Type
function

id: touchablenativefeedback title: 触摸原生反馈

onPressOut

当触摸释放时立即调用(甚至先于 onPress)。首个函数参数为 PressEvent 类型事件。

Type
function

id: touchablenativefeedback title: 触摸原生反馈

pressRetentionOffset

当滚动视图禁用时,此属性定义触摸点可偏离按钮多远才会使按钮失活。失活后往回移动可重新激活按钮!在滚动视图禁用状态下多次往返移动可验证此效果。建议传递常量以减少内存分配。

Type
Rect or number

id: touchablenativefeedback title: 触摸原生反馈

nativeID

Type
string

id: touchablenativefeedback title: 触摸原生反馈

testID(测试标识)

用于在端到端测试中定位此视图。

Type
string

id: touchablenativefeedback title: 触摸原生反馈

touchSoundDisabled
Android

若为 true,触摸时不播放系统提示音。

Type
Boolean