zoukankan      html  css  js  c++  java
  • react native 之 获取键盘高度

    多说不如多撸:

    
    
    /**
    * Created by shaotingzhou on 2017/2/23.
    */
    /**
    * Sample React Native App
    * https://github.com/facebook/react-native
    * @flow
    */

    import React, { Component } from 'react';
    import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Keyboard,
    TextInput,
    Dimensions
    } from 'react-native';
    var ScreenWidth = Dimensions.get('window').width;

    export default class Root extends Component {
    // 构造
    constructor(props) {
    super(props);
    // 初始状态
    this.state = {
    keyboardHeight:0
    };
    }
    render() {
    return (
    <View style={styles.container}>
    <Text style={styles.welcome}>
    Welcome to React Native!
    </Text>
    <Text style={styles.instructions}>
    To get started, edit index.android.js
    </Text>
    <Text style={styles.instructions}>
    Double tap R on your keyboard to reload,{' '}
    Shake or press menu button for dev menu
    </Text>
    <Text style={styles.instructions}>
    To get started, edit index.android.js
    </Text>
    <Text style={styles.instructions}>
    Double tap R on your keyboard to reload,{' '}
    Shake or press menu button for dev menu
    </Text>
    <Text style={styles.instructions}>
    To get started, edit index.android.js
    </Text>
    <Text style={styles.instructions}>
    Double tap R on your keyboard to reload,{' '}
    Shake or press menu button for dev menu
    </Text>
    <Text style={styles.instructions}>
    To get started, edit index.android.js
    </Text>
    <Text style={styles.instructions}>
    Double tap R on your keyboard to reload,{' '}
    Shake or press menu button for dev menu
    </Text>
    <TextInput
    style={{ScreenWidth,height:100,borderWidth:2,marginBottom:this.state.keyboardHeight}}
    />
    </View>
    );
    }

    componentWillUnmount() {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
    }

    componentWillMount() {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));
    }

    _keyboardDidShow(e){
    this.setState({
    keyboardHeight:e.startCoordinates.height
    })

    }

    _keyboardDidHide(e){
    this.setState({
    keyboardHeight:0
    })
    }
    }

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    },
    welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    },
    instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
    },
    });
     

    效果:

    额.后来发现个KeyboardAvoidingView,原来react native已经意识到了.所以上面的代码可以作废.使用新的KeyboardAvoidingView,其中

    KeyboardAvoidingView的主要属性behavior  包含三个'height', 'position', 'padding'

    大致代码如下:

      1 /**
      2  * Sample React Native App
      3  * https://github.com/facebook/react-native
      4  * @flow
      5  */
      6 
      7 import React, { Component } from 'react';
      8 import {
      9     AppRegistry,
     10     StyleSheet,
     11     Text,
     12     View,
     13     ScrollView,
     14     TextInput,
     15     KeyboardAvoidingView
     16 } from 'react-native';
     17 
     18 export default class Root extends Component {
     19     render() {
     20         return (
     21             <KeyboardAvoidingView behavior='position' >
     22                 <ScrollView>
     23                     <View style={styles.container}>
     24                         <Text style={styles.welcome}>
     25                             Welcome to React Native!
     26                         </Text>
     27                         <Text style={styles.instructions}>
     28                             To get started, edit index.ios.js
     29                         </Text>
     30                         <Text style={styles.instructions}>
     31                             Press Cmd+R to reload,{'
    '}
     32                             Cmd+D or shake for dev menu
     33                         </Text>
     34                         <Text style={styles.welcome}>
     35                             Welcome to React Native!
     36                         </Text>
     37                         <Text style={styles.instructions}>
     38                             To get started, edit index.ios.js
     39                         </Text>
     40                         <Text style={styles.instructions}>
     41                             Press Cmd+R to reload,{'
    '}
     42                             Cmd+D or shake for dev menu
     43                         </Text>
     44                         <Text style={styles.welcome}>
     45                             Welcome to React Native!
     46                         </Text>
     47                         <Text style={styles.instructions}>
     48                             To get started, edit index.ios.js
     49                         </Text>
     50                         <Text style={styles.instructions}>
     51                             Press Cmd+R to reload,{'
    '}
     52                             Cmd+D or shake for dev menu
     53                         </Text>
     54                         <Text style={styles.welcome}>
     55                             Welcome to React Native!
     56                         </Text>
     57                         <Text style={styles.instructions}>
     58                             To get started, edit index.ios.js
     59                         </Text>
     60                         <Text style={styles.instructions}>
     61                             Press Cmd+R to reload,{'
    '}
     62                             Cmd+D or shake for dev menu
     63                         </Text>
     64                         <Text style={styles.welcome}>
     65                             Welcome to React Native!
     66                         </Text>
     67                         <Text style={styles.instructions}>
     68                             To get started, edit index.ios.js
     69                         </Text>
     70                         <Text style={styles.instructions}>
     71                             KeyboardAvoidingView的主要属性behavior  PropTypes.oneOf(['height', 'position', 'padding'])
     72                         </Text>
     73                         <TextInput
     74                             placeholder="输入框"
     75                             style={{300,height:100,borderWidth:1}}
     76                         />
     77                     </View>
     78                 </ScrollView>
     79             </KeyboardAvoidingView>
     80         );
     81     }
     82 }
     83 
     84 const styles = StyleSheet.create({
     85     container: {
     86         flex: 1,
     87         justifyContent: 'center',
     88         alignItems: 'center',
     89         backgroundColor: '#F5FCFF',
     90     },
     91     welcome: {
     92         fontSize: 20,
     93         textAlign: 'center',
     94         margin: 10,
     95     },
     96     instructions: {
     97         textAlign: 'center',
     98         color: '#333333',
     99         marginBottom: 5,
    100     },
    101 });

     效果:

  • 相关阅读:
    Thinkphp框架网站 nginx环境 访问页面access denied
    jenkins 构建触发器 Poll SCM 和 Build periodically区别
    jenkins持续化集成工具 centos 6.5安装
    centos 6.5升级内核到3.1
    awk常见用法
    html手机网页自适应宽度
    centos 6.8安装java环境
    论mysql主从复制里面的那些坑
    redis持久化
    spring配置日志
  • 原文地址:https://www.cnblogs.com/shaoting/p/6432386.html
Copyright © 2011-2022 走看看