React Native 第三章

89 阅读3分钟

📝 一、TextInput — 文本输入框

TextInput 是 React Native 中的基础输入组件,用于让用户输入文本内容。
类似于 Web 中的 <input />

✅ 基本用法

import React, { useState } from 'react';
import { View, TextInput, Text } from 'react-native';

export default function App() {
  const [text, setText] = useState('');

  return (
    <View style={{ padding: 20 }}>
      <Text>输入内容:</Text>
      <TextInput
        style={{
          height: 40,
          borderColor: 'gray',
          borderWidth: 1,
          paddingHorizontal: 8,
        }}
        placeholder="请输入内容"
        value={text}
        onChangeText={setText}
      />
      <Text>当前输入:{text}</Text>
    </View>
  );
}

⚙️ 常用属性

属性名类型说明
valuestring当前输入值(受控)
defaultValuestring初始值(非受控)
placeholderstring占位文字
onChangeText(text: string) => void文本变化时回调
keyboardTypestring键盘类型,如 default / numeric / email-address / phone-pad
secureTextEntryboolean是否密码输入(隐藏文字)
maxLengthnumber最大输入长度
editableboolean是否可编辑
multilineboolean多行输入(类似 <textarea>
autoFocusboolean自动聚焦
onFocus / onBlurfunction获取或失去焦点事件
returnKeyTypestring键盘右下角按钮文字,如 "done", "go", "search"
styleobject样式定义

💡Tips

  • onSubmitEditing 在按下“完成”或“回车”键时触发;

  • blurOnSubmit={false} 可以在多行输入时防止键盘收起;

  • 可结合 useRef 获取焦点:

    const inputRef = useRef();
    inputRef.current.focus();
    

👆 二、TouchableOpacity — 可点击且点击时透明度变化

TouchableOpacity 是最常用的点击容器组件。
点击时会有轻微的 透明度变化,适合自定义按钮。

✅ 基本用法

import React from 'react';
import { TouchableOpacity, Text, Alert } from 'react-native';

export default function App() {
  return (
    <TouchableOpacity
      style={{
        backgroundColor: '#2196F3',
        padding: 10,
        borderRadius: 6,
      }}
      activeOpacity={0.7}
      onPress={() => Alert.alert('按钮被点击!')}
    >
      <Text style={{ color: '#fff', textAlign: 'center' }}>点我一下</Text>
    </TouchableOpacity>
  );
}

⚙️ 常用属性

属性名类型说明
onPressfunction点击事件
onLongPressfunction长按事件
activeOpacitynumber点击时的不透明度(0 ~ 1,默认0.2)
disabledboolean是否禁用
styleobject样式定义
delayPressIn / delayPressOutnumber延迟响应点击时间(毫秒)

💡Tips

  • 可嵌套任意子元素(如 View, Text, Image);
  • 用于按钮、卡片点击、图片点击等场景;
  • 对应 Web 习惯中 <button><div onClick> 的作用。

✨ 三、TouchableHighlight — 点击时背景高亮

TouchableHighlight 点击时会显示**背景高亮(变色)**效果。
适合需要明显按压反馈的 UI 元素(如列表项)。

✅ 基本用法

import React from 'react';
import { TouchableHighlight, Text, View, Alert } from 'react-native';

export default function App() {
  return (
    <TouchableHighlight
      underlayColor="#DDDDDD"
      onPress={() => Alert.alert('高亮按钮被点击!')}
      style={{
        backgroundColor: '#2196F3',
        borderRadius: 6,
        padding: 10,
      }}
    >
      <Text style={{ color: '#fff', textAlign: 'center' }}>高亮按钮</Text>
    </TouchableHighlight>
  );
}

⚙️ 常用属性

属性名类型说明
onPressfunction点击事件
underlayColorstring点击时显示的高亮背景色
activeOpacitynumber点击时子元素透明度变化
onShowUnderlay / onHideUnderlayfunction高亮显示或隐藏时回调
disabledboolean禁用点击
styleobject样式定义

💡Tips

  • TouchableOpacity 视觉反馈更明显;
  • 适合 列表项点击效果卡片点击高亮
  • 一般不推荐嵌套过多子组件,否则高亮效果可能异常。

📚 对比总结

特性TextInputTouchableOpacityTouchableHighlight
用途输入文本点击触发事件点击触发事件(带高亮)
交互反馈输入框光标/键盘透明度变化背景高亮
常用场景登录输入框、搜索框普通按钮、图标点击列表项、卡片点击
可嵌套子元素否(仅文本)
推荐度★★★★☆★★★★★★★★☆☆