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

如何在React Native函数式组件中使用beefe/react-native-keyboard?

React Native 自定义数字键盘问题解决方案

1. 类组件集成时"_notify不存在"错误修复

你遇到的_notify不存在错误,大概率是组件内部上下文绑定异常导致的。别用bind(this)的方式绑定回调,直接改用类属性箭头函数定义处理方法,能避免上下文丢失问题:

class YourComponent extends React.Component {
  // 用箭头函数定义处理方法,自动绑定this
  _handleClear = () => {
    // 你的清除逻辑
  }

  _handleDelete = () => {
    // 你的删除逻辑
  }

  _handleKeyPress = (key) => {
    // 你的按键逻辑,key是按下的数字/小数点
  }

  render() {
    return (
      <View>
        <Keyboard 
          keyboardType="decimal-pad"
          onClear={this._handleClear}
          onDelete={this._handleDelete}
          onKeyPress={this._handleKeyPress}
        />
      </View>
    )
  }
}

另外给你补一个函数式组件的使用示例,用React Hooks就能轻松实现:

import React from 'react';
import { View } from 'react-native';
import Keyboard from 'beefe/react-native-keyboard';

const NumberInputScreen = () => {
  const handleClear = () => {
    // 清除输入逻辑
  };

  const handleDelete = () => {
    // 删除最后一位逻辑
  };

  const handleKeyPress = (key) => {
    // 处理按键输入,比如更新状态
    console.log('按下的键:', key);
  };

  return (
    <View>
      <Keyboard
        keyboardType="decimal-pad"
        onClear={handleClear}
        onDelete={handleDelete}
        onKeyPress={handleKeyPress}
      />
    </View>
  );
};

export default NumberInputScreen;

2. Decimal模式+secureTextEntry导致数字被当作密码处理的解决

secureTextEntry={true}本身就是用来隐藏输入内容的,和decimal模式混用自然会把数字变成密码圆点。如果你需要输入数字但加密显示,别依赖组件自带的secureTextEntry,手动处理显示逻辑即可:

  1. 用状态存储真实的数字输入值
  2. 显示时把真实值转换成等长的*或其他掩码字符
  3. 键盘输入正常处理真实值

示例逻辑:

const [realValue, setRealValue] = useState('');

const handleKeyPress = (key) => {
  // 处理数字/小数点输入,更新真实值
  setRealValue(prev => prev + key);
};

// 显示时用掩码替代真实值
<Text>{'*'.repeat(realValue.length)}</Text>

如果只是不想让数字被隐藏,直接去掉secureTextEntry={true}就行,decimal模式本身只允许数字和小数点输入。

3. Material Tab Bar中ref.focus()导致UI闪烁的修复

Tab Bar切换时组件会重新渲染,直接调用ref.focus()会触发不必要的UI重绘导致闪烁,可以试试这几个方案:

  • 用useRef保持稳定引用:确保ref不会因组件重渲染被重新创建
  • 延迟focus操作:在Tab切换完成后再执行focus,比如用setTimeout或监听Tab切换事件
  • 用React.memo包裹输入组件:避免无关重渲染

示例代码(函数式组件):

import { useRef, useEffect } from 'react';
import { TabView } from 'react-native-tab-view';

const TabScreen = () => {
  const inputRef = useRef(null);
  const [index, setIndex] = useState(0);

  useEffect(() => {
    // 切换到目标页签后延迟100ms执行focus,避免闪烁
    if (index === 0) { // 假设第一个页签是数字输入页
      setTimeout(() => {
        inputRef.current?.focus();
      }, 100);
    }
  }, [index]);

  return (
    <TabView
      navigationState={{ index, routes }}
      onIndexChange={setIndex}
      renderScene={({ route }) => {
        if (route.key === 'numberInput') {
          return (
            <View>
              {/* 你的输入组件 */}
              <TextInput ref={inputRef} keyboardType="decimal-pad" />
              <Keyboard />
            </View>
          );
        }
        // 其他页签内容
      }}
    />
  );
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:12:45