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,{'\n'}
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,{'\n'}
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,{'\n'}
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,{'\n'}
Shake or press menu button for dev menu
</Text>
<TextInput
style={{width: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,{'\n'}
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,{'\n'}
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,{'\n'}
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,{'\n'}
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={{width: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,
},
});
react native 获取 软键盘高度 和 新增软键盘的组件的更多相关文章
- [RN] React Native 获取地理位置
React Native 获取地理位置 实现原理: 1.用 navigator.geolocation.getCurrentPosition 获取到坐标信息 2.调用 高德地图 接口,解析位置数据 ...
- [RN] React Native 获取验证码 按钮
React Native 获取验证码 按钮 效果如图: 实现方法: 一.获取验证码 按钮组件 封装 CountDownButton.js "use strict"; import ...
- React Native 获取组件(Component)在屏幕上的位置
年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...
- [React Native]获取网络状态
使用React Native,可以使用NetInfo API获取手机当前的各个网络状态. componentWillMount() { NetInfo.fetch().done((status)=&g ...
- React Native 获取网络数据
getMoviesFromApiAsync() { return fetch('http://facebook.github.io/react-native/movies.json') .then(( ...
- react native获取组件高度,宽 度等
import React,{Component} from 'react'import { View, Text, Image, StyleSheet, TouchableHighlight, Tou ...
- react native获取屏幕的宽度和高度
var Dimensions = require('Dimensions'); var {width,height} = Dimensions.get("window");//第一 ...
- React Native获取组件位置和大小
RN页面中定位或滚动操作时,需要获取元素的大小和位置信息,有几种常用的方法 获取设备屏幕的宽高 import {Dimensions} from 'react-native'; var {height ...
- react native 获取地图需要的SHA1
1.从电脑的根目录进入.android文件 2.进入.android文件后输入 keytool -v -list -keystore debug.keystore 3.回车输入密码,(可以直接回车不用 ...
随机推荐
- 3.4 Templates -- Displaying A List of Items(展示一个集合)
一. 概述 1. example 如果你需要遍历一个对象集合,使用Handlebars的{{#each}}. <ul> {{#each people key="id" ...
- MD5—加密,加盐
MD5的参考盐值:String salt = "212*)()()**()^&UYGbakdkj " ; MD5—加密工具类 package com.demo.tools; ...
- rails性能优化
1,使用Unicorn或者Thin服务器替代默认的webrick.2,静态资源压缩合并,放到云存储上.3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js ...
- 37QT程序打包
Qt的应用程序编译出来之后,将单独的exe程序拿到其他PC上运行是运行不起来的,会提示缺少对应的动态链接库.我们需要去Qt的安装目录下找到所有的Qt程序运行时所依赖的,将他们和exe程序放到同一目录下 ...
- Java与Flex学习笔记(20)---将flex页面嵌入到jsp页面中
如果我们只需要用到Flex的一部分功能,例如播放器功能,我们可以单独把Flex页面嵌入到Jsp页面中.要想实现此功能,需要下载一个工程,将其覆盖在服务器根目录下即可.你可以在次下载:FlexModul ...
- 初识PHP(三)面向对象特性
PHP5开始支持面向对象的编程方式.PHP的面向对象编程方法和别的语言区别不大,下面对PHP面向编程基本语法进行简单记录. 一.声明对象 声明方法: class Say{ public functio ...
- hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办
1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ...
- MVC webapi,Action的分别拦截器
继承Controller,ApiController的拦截继承的接口不一样 webapi的拦截器:(继承System.Web.Http) using System; using System.Coll ...
- Github 下载项目的某一分支版本
参考:如何在 GitHub 下载某个程序的特定版本(代码)? 在安装ntf做int实验的时候,发现int demo的这个repo是该项目的一个branch,与master分支不一致(5 commits ...
- 比较两个JSON字符串是否完全相等
RT,比较两个JSON字符串是否完全相等,这里使用google贡献的Gson. 一,no POJO,即不另外创建一个简单Java类 [java] view plain copy String str1 ...