javascript 关闭键盘 React-Native
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32876720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dismiss Keyboard React-Native
提问by zafrani
Is there a simple way to hide the iOS keyboard? I want to force it shut in a few specific scenarios that don't necessarily require focusing another view. i.e. like a specific response from the server.
有没有一种简单的方法可以隐藏 iOS 键盘?我想在一些不一定需要关注另一个视图的特定场景中强制关闭它。即像来自服务器的特定响应。
回答by Chen-Tsu Lin
Use built-in Keyboard
Module:
使用内置Keyboard
模块:
import { Keyboard } from 'react-native';
Keyboard.dismiss();
回答by zafrani
Was able to achieve this with
能够做到这一点
import dismissKeyboard from 'react-native/Libraries/Utilities/dismissKeyboard'
And then at the point where I need to toggle the keyboard off
dismissKeyboard();
-- -- EDIT -- --
Importing like this works equally as well.
import dismissKeyboard from 'dismissKeyboard'
import dismissKeyboard from 'react-native/Libraries/Utilities/dismissKeyboard'
然后在我需要关闭键盘的地方
dismissKeyboard();
- - 编辑 - -
像这样导入也同样有效。
import dismissKeyboard from 'dismissKeyboard'
-- -- EDIT #2 -- --
-- -- 编辑#2 -- --
My original answer is now outdated. The correct way is mentioned below by @Chen-Tsu Lin
我原来的答案现在已经过时了。@Chen-Tsu Lin 下面提到了正确的方法
import { Keyboard } from 'react-native';
Keyboard.dismiss();
回答by Daniel Schmidt
You may also use this library I wrote: react-native-dismiss-keyboard. It does basically the same as the import of 'dismissKeyboard', but it only uses public API and is therefore safer to use and does not rely on the packager resolving 'dismissKeyboard' as it currently does.
你也可以使用我写的这个库:react-native-dismiss-keyboard。它与“dismissKeyboard”的导入基本相同,但它仅使用公共 API,因此使用起来更安全,并且不像目前那样依赖打包器解析“dismissKeyboard”。