• 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, }, });

      

    链接

  • 相关阅读:
    LeetCode题解 | [简单-数组] 485.最大连续1的个数
    PAT乙级真题 | 1032 挖掘机技术哪家强
    [leetcode]两个列表的最小索引总和
    【leetCode】两个数组的交集
    手写hashMap(非红黑树)
    Redis 删除数据后不能自动释放内存的问题
    Spring @Async/@Transactional 失效的原因及解决方案
    完全平方数问题
    用队列实现栈
    memcached安装踩坑
  • 原文地址:https://www.cnblogs.com/dhsz/p/6478671.html
Copyright © 2020-2023  润新知