rn 手动隐藏键盘、点击空白处隐藏键盘

今天药忘吃喽~ 2021-07-24 14:21 1370阅读 0赞
  1. 1、导入
  2. import {Keyboard} from 'react-native';
  3. 2、手动隐藏键盘
  4. 在事件中设置:Keyboard.dismiss();
  5. 3、点击空白处隐藏键盘
  6. 方式一:
  7. 使用ScrollView作为父及以上容器
  8. 方式二:
  9. 使用TouchableWithoutFeedback组件,该按钮组件无任何视觉反馈效果
  10. 使用TouchableWithoutFeedback在外层包裹,onPress事件中设置Keyboard.dismiss();
  11. 子元素要占据全屏

代码示例:
使用ScrollView作为父及以上容器

  1. import React,{ Component} from 'react'
  2. import {
  3. View,
  4. Text,
  5. StyleSheet,
  6. Button,
  7. TouchableOpacity,
  8. Image,
  9. TextInput,
  10. Keyboard,
  11. TouchableWithoutFeedback,
  12. ScrollView
  13. } from 'react-native'
  14. import TopBar from '../../components/common/Topbar'
  15. class Search extends Component{
  16. state={
  17. value:''
  18. }
  19. renderLeft()
  20. {
  21. return (
  22. <Button
  23. title='<返回'
  24. onPress={ ()=>{
  25. this.props.navigation.goBack()
  26. }}
  27. />
  28. )
  29. }
  30. renderMiddle()
  31. {
  32. return (
  33. <TouchableOpacity>
  34. <Text>搜索全网折扣</Text>
  35. </TouchableOpacity>
  36. )
  37. }
  38. render()
  39. {
  40. return(
  41. <View style={ { backgroundColor:'white',flex:1}}>
  42. <TopBar
  43. leftItem={ this.renderLeft.bind(this)}
  44. middleItem={ this.renderMiddle.bind(this)}
  45. />
  46. <ScrollView>
  47. { /* <TouchableWithoutFeedback onPress={()=>{ Keyboard.dismiss() }} > */}
  48. <View style={ styles.inp}>
  49. <TextInput
  50. placeholder='搜索内容'
  51. selectionColor='green'
  52. style={ styles.ipt}
  53. value={ this.state.value}
  54. onChangeText={ (text)=>{
  55. this.setState({
  56. value:text
  57. })
  58. }}
  59. numberOfLines = { 1}
  60. clearButtonMode='while-editing'
  61. />
  62. <TouchableOpacity
  63. onPress={ ()=>{
  64. this.setState({
  65. value:''
  66. })
  67. Keyboard.dismiss()
  68. }}
  69. >
  70. <Text style={ styles.txt}>取消</Text>
  71. </TouchableOpacity>
  72. </View>
  73. </ScrollView>
  74. { /* </TouchableWithoutFeedback> */}
  75. </View>
  76. )
  77. }
  78. }
  79. const styles = StyleSheet.create({
  80. inp:{
  81. flexDirection:'row',
  82. justifyContent:'center',
  83. marginTop:20,
  84. flex:1
  85. },
  86. ipt:{
  87. flex:1,
  88. height:40,
  89. backgroundColor:'#EDEAEF',
  90. borderRadius:10,
  91. marginLeft:10,
  92. marginRight:10
  93. },
  94. txt:{
  95. height:40,
  96. lineHeight:40,
  97. color:'green'
  98. }
  99. })
  100. export default Search

TouchableWithoutFeedback组件

  1. import React,{ Component} from 'react'
  2. import {
  3. View,
  4. Text,
  5. StyleSheet,
  6. Button,
  7. TouchableOpacity,
  8. Image,
  9. TextInput,
  10. Keyboard,
  11. TouchableWithoutFeedback,
  12. ScrollView
  13. } from 'react-native'
  14. import TopBar from '../../components/common/Topbar'
  15. class Search extends Component{
  16. state={
  17. value:''
  18. }
  19. renderLeft()
  20. {
  21. return (
  22. <Button
  23. title='<返回'
  24. onPress={ ()=>{
  25. this.props.navigation.goBack()
  26. }}
  27. />
  28. )
  29. }
  30. renderMiddle()
  31. {
  32. return (
  33. <TouchableOpacity>
  34. <Text>搜索全网折扣</Text>
  35. </TouchableOpacity>
  36. )
  37. }
  38. render()
  39. {
  40. return(
  41. <View style={ { backgroundColor:'white',flex:1}}>
  42. <TopBar
  43. leftItem={ this.renderLeft.bind(this)}
  44. middleItem={ this.renderMiddle.bind(this)}
  45. />
  46. { /* <ScrollView> */}
  47. <TouchableWithoutFeedback
  48. onPress={ ()=>{
  49. Keyboard.dismiss()
  50. }}
  51. >
  52. <View style={ styles.inp}>
  53. <TextInput
  54. placeholder='搜索内容'
  55. selectionColor='green'
  56. style={ styles.ipt}
  57. value={ this.state.value}
  58. onChangeText={ (text)=>{
  59. this.setState({
  60. value:text
  61. })
  62. }}
  63. numberOfLines = { 1}
  64. clearButtonMode='while-editing'
  65. />
  66. <TouchableOpacity
  67. onPress={ ()=>{
  68. this.setState({
  69. value:''
  70. })
  71. Keyboard.dismiss()
  72. }}
  73. >
  74. <Text style={ styles.txt}>取消</Text>
  75. </TouchableOpacity>
  76. </View>
  77. { /* </ScrollView> */}
  78. </TouchableWithoutFeedback>
  79. </View>
  80. )
  81. }
  82. }
  83. const styles = StyleSheet.create({
  84. inp:{
  85. flexDirection:'row',
  86. justifyContent:'center',
  87. marginTop:20,
  88. flex:1
  89. },
  90. ipt:{
  91. flex:1,
  92. height:40,
  93. backgroundColor:'#EDEAEF',
  94. borderRadius:10,
  95. marginLeft:10,
  96. marginRight:10
  97. },
  98. txt:{
  99. height:40,
  100. lineHeight:40,
  101. color:'green'
  102. }
  103. })
  104. export default Search

发表评论

表情:
评论列表 (有 0 条评论,1370人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android隐藏键盘

    网上好多方法说的隐藏方法,其实是隐藏/显示方法,即,当前键盘显示,调用一下,隐藏,在调用一下,又显示了。下面提供两种彻底隐藏的方法: / 软键盘显