Dismiss keyboard in multiline TextInput in React native

后端 未结 5 1378
悲&欢浪女
悲&欢浪女 2020-12-14 15:29

When the user presses the Return key in a multiline TextInput, a new line is created and the keyboard continues to be visible. How should the keybo

5条回答
  •  天涯浪人
    2020-12-14 15:44

    Banged my head against this for a few hours and after fiddling around and some dumb luck finally figured out how to make a multiline TextInput dismiss by just touching outside the text box. Little Example for ya'll hope that someone else finds this useful because the docs do not make it all apparent that you can get multilines to dismiss easily...

    import React, { Component}  from 'react'`
    import {
    keyboardAvoidingView,
    Keyboard,
    StatusBar,
    StyleSheet,
    TextInput,
    View,
    } from 'react-native';
    
    class App extends Component {
    constructor(props){
    super(props)
    this.state ={
            behavior: 'position',
    }
    this._keyboardDismiss =  this._keyboardDismiss.bind(this);
    }
    componentWillMount() {
    this.keyboardDidHideListener = 
                      Keyboard
                     .addListener('keyboardDidHide',this._keyboardDidHide);                                                         
                  }
    
    componentWillUnmount(){
    this.keyboardDidHideListener.remove();
    }
    
    _keyboardDidHide() {
    Keyboard.dismiss();
    }
    
    render() {
    
    return (
        
        
        
          
        
        
    
        
      );
     }
    }
    

提交回复
热议问题