zoukankan      html  css  js  c++  java
  • react native 获取 软键盘高度 和 新增软键盘的组件

      
    
    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. 开始 start 口 Coordin 订 ates.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'

    大致代码如下:

    /**
     * Sample React Native App
     * https://github.com/facebook/react-native
     * @flow
     */
    
    import React, { Component } from 'react';
    import { AppRegistry, StyleSheet, Text, View, ScrollView, TextInput, KeyboardAvoidingView
    } from 'react-native'; export default class Root extends Component { render() { return ( <KeyboardAvoidingView behavior='position' > <ScrollView> <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.ios.js </Text> <Text style={styles.instructions}> Press Cmd+R to reload,{' '} Cmd+D or shake for dev menu </Text> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.ios.js </Text> <Text style={styles.instructions}> Press Cmd+R to reload,{' '} Cmd+D or shake for dev menu </Text> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.ios.js </Text> <Text style={styles.instructions}> Press Cmd+R to reload,{' '} Cmd+D or shake for dev menu </Text> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.ios.js </Text> <Text style={styles.instructions}> Press Cmd+R to reload,{' '} Cmd+D or shake for dev menu </Text> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.ios.js </Text> <Text style={styles.instructions}> KeyboardAvoidingView的主要属性behavior PropTypes.oneOf(['height', 'position', 'padding']) </Text> <TextInput placeholder="输入框" style={{300,height:100,borderWidth:1}} /> </View> </ScrollView> </KeyboardAvoidingView> ); } } 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, }, });

      

    链接

  • 相关阅读:
    【工具类】图片压缩工具类,可压缩jpg, png等图片
    【Nginx用法】nginx location正则表达式写法,详解Nginx location 匹配规则(很详细哦)
    【Nginx异常】[error] 4236#29900: OpenEvent(“Global gx_reload_27128“) failed (5: Access is denied)
    【Nginx异常】Nginx启动一闪而过没反应,Nginx双击打开后,没有启动成功,也没有进程,且127.0.0.1:8080访问不到
    开启vue-element-ui打包生成报告
    Cas 5.2.x 使用 实现SSO单点登录的问题
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题
    企业微信会话存档开发与问题
    高手怎么查找CPU过高的Java代码。具体到行
    ubuntu中清除开始菜单中的应用图标
  • 原文地址:https://www.cnblogs.com/dhsz/p/6478671.html
Copyright © 2011-2022 走看看