react-native - 如何在自定义 react-native TextInput 中实现焦点/模糊响应
问题描述
我实现了一个由本地库支持的自定义react-native TextInput。它工作得很好,只是当我在文本字段之外点击时,它不会自动模糊并且键盘也不会消失。我也试过了Keyboard.dismiss()
,它也不起作用。我查看了“官方”TextInput
实现来复制它,但没有成功。
我在我的自定义实现中添加了这段代码(componentDidMount
)
if (this.context.focusEmitter) {
this._focusSubscription = this.context.focusEmitter.addListener(
'focus',
el => {
if (this === el) {
this.requestAnimationFrame(this.focus);
} else if (this.isFocused()) {
this.blur();
}
},
);
if (this.props.autoFocus) {
this.context.onFocusRequested(this);
}
} else {
if (this.props.autoFocus) {
this.requestAnimationFrame(this.focus);
}
}
我还定义了所需的 contextTypes
static contextTypes = {
focusEmitter: PropTypes.instanceOf(EventEmitter)
}
我遇到的问题focusEmitter
是在上下文中是未定义的,我不知道它是从哪里提供的,也不知道它是否真的适用于常规的TextInput
. focusEmitter
我可以在 react-native 存储库中找到的唯一出现是在 NavigatorIOS 中,我什至没有在我的应用程序中使用它。谁能向我澄清这一点?
解决方案
做你想做的事情的更简单的方法是Keyboard.dismiss()
在TouchableWithoutFeedback
下面的例子中使用:
import {Keyboard} from 'react-native';
...
render(){
return(
<TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
<View>
// Return everything here
<TextInput />
</View>
</TouchableWithoutFeedback>
)
}
因此,当您在输入之外点击时,它将关闭键盘并模糊TextInput
.
推荐阅读
- java - ubuntu 上的 java 线程中的 jni 内存泄漏
- c# - 如何在 C# Visual Studio 中替换用于单元测试的 dll 库?
- .net - 如何在 Visual Studio 中自动更新 ErrorList?
- python - QObject::connect: 无法对“QTextCursor”类型的参数进行排队(确保使用 qRegisterMetaType() 注册了“QTextCursor”。)
- laravel - Laravel 没有在视图中显示图像
- swift - 设置 UITabBarItem 属性文本不起作用
- sql-server - 将变量插入 XML 节点搜索
- javafx - While 循环导致我的 JavaFX 应用程序崩溃
- python - 将赤道坐标转换为 alt-az 坐标非常慢
- bash - CLOSE_WAIT 状态的客户端连接可以从命令行(服务器外部)终止吗?