1.通过Keyboard获取键盘高度,改变定位的bottom

  缺点:虚拟键盘完全弹起时,才会获取到键盘高度,定位稍有延迟,而且键盘收起时,定位会出现悬空状态,然后再回到底部

import React, { Component } from 'react'
import {
View,
Text,
Button,
Keyboard,
Platform,
TextInput,
StyleSheet,
ScrollView,
} from 'react-native' import rpx from '../util/rpx' export default class Home extends Component {
constructor(props) {
super(props)
this.state = {
keyBoardHeight: 0
}
}
componentWillMount() {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
_keyboardDidShow(e) {
this.setState({
keyBoardHeight: e.endCoordinates.height
});
}
_keyboardDidHide() {
this.setState({
keyBoardHeight: 0
});
}
loseFocus() {
this.refs.input.blur()
}
render() {
let { keyBoardHeight } = this.state
return (
<View style={css.container}>
<ScrollView style={{paddingBottom: rpx(100)}}>
<Text>键盘高度: {keyBoardHeight}</Text>
<View style={{backgroundColor: 'red', height: rpx(500)}}></View>
<Button title="收起键盘" style={css.txt} onPress={() => this.loseFocus()}></Button>
<View style={{backgroundColor: 'blue', height: rpx(500)}}></View>
<View style={{backgroundColor: 'green', height: rpx(500)}}></View>
</ScrollView>
<View style={[css.box, Platform.OS == 'ios' && { bottom: keyBoardHeight }]}>
<TextInput
ref="input"
style={css.input}
placeholderTextColor='#999999'
placeholder={'输入代码、名称或简拼'}
underlineColorAndroid="transparent" />
</View>
</View>
)
}
} const css = StyleSheet.create({
container: {
flex: 1
},
input: {
height: rpx(60),
width: '100%',
fontSize: rpx(26),
color: '#333333',
backgroundColor: '#eee',
borderRadius: rpx(60),
paddingHorizontal: rpx(20),
paddingVertical: 0
},
box: {
width: rpx(750),
height: rpx(100),
backgroundColor: '#fff',
paddingHorizontal: rpx(30),
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
txt: {
color: 'red'
}
})

  2.通过KeyboardAvoidingView组件,将页面推送上去,完美解决

import React, { Component } from 'react'
import {
View,
Text,
Button,
Platform,
TextInput,
StyleSheet,
ScrollView,
KeyboardAvoidingView
} from 'react-native' import rpx from '../util/rpx' export default class Home extends Component {
constructor(props) {
super(props)
this.state = {
}
}
loseFocus() {
this.refs.input.blur()
}
render() {
let behavior = Platform.OS == 'ios' ? 'position' : null
return (
<KeyboardAvoidingView style={css.container} behavior={behavior}>
<ScrollView style={{paddingBottom: rpx(100)}}>
<View style={{backgroundColor: 'red', height: rpx(500)}}></View>
<Button title="收起键盘" style={css.txt} onPress={() => this.loseFocus()}></Button>
<View style={{backgroundColor: 'blue', height: rpx(500)}}></View>
<View style={{backgroundColor: 'green', height: rpx(500)}}></View></ScrollView>
<View style={[css.box]}>
<TextInput
ref="input"
style={css.input}
placeholderTextColor='#999999'
placeholder={'输入代码、名称或简拼'}
underlineColorAndroid="transparent" />
</View>
</KeyboardAvoidingView>
)
}
} const css = StyleSheet.create({
container: {
flex: 1
},
input: {
height: rpx(60),
width: '100%',
fontSize: rpx(26),
color: '#333333',
backgroundColor: '#eee',
borderRadius: rpx(60),
paddingHorizontal: rpx(20),
paddingVertical: 0
},
box: {
width: rpx(750),
height: rpx(100),
backgroundColor: '#fff',
paddingHorizontal: rpx(30),
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
txt: {
color: 'red'
}
})

react native输入框定位在底部(虚拟键盘弹起)的更多相关文章

  1. React Native(四)——顶部以及底部导航栏实现方式

    效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...

  2. Swift - 点击输入框外部屏幕关闭虚拟键盘

    我们如果把文本框的Return Key设置成Done,然后在storyboard中将文本框的Did End On Exit事件在代码里进行关联.同时关联代码里调用文本框的resignFirstResp ...

  3. React Native 的组件之底部导航栏 TabBarIOS(一)

    import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, TabBarIOS, } fro ...

  4. apicloud iphoneX底部虚拟键盘遮挡

    1.首先,底部的高不能写死. 2. var footer = $api.byId('footer'); $api.fixTabBar(footer);这句应该写在 footerHeight = $ap ...

  5. 移动端fixed定位在底部,出现键盘后消失

    jq var h=$(window).height(); $(window).resize(function() { if($(window).height()<h){ $('.nav').hi ...

  6. Android适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  7. Android 适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  8. 虚拟键盘,移动web开发的痛

    原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗?  你可能会说着:“不就是放一个position: fixed;的输入框在 ...

  9. React Native 从入门到原理

    React Native 是最近非常火的一个话题,介绍如何利用 React Native 进行开发的文章和书籍多如牛毛,但面向入门水平并介绍它工作原理的文章却寥寥无几. 本文分为两个部分:上半部分用通 ...

随机推荐

  1. 一个用来爬小说的简单的Node.js爬虫

    小说就准备点天下霸唱和南派三叔的系列,本人喜欢看,而且数据也好爬.貌似因为树大招风的原因,这两作者的的书被盗版的很多,乱改的也多.然后作者就直接在网上开放免费阅读了,还提供了官网,猜想作者应该是允许爬 ...

  2. vue-cli3安装过程

    作为一个本来是java开发的搬运工,在公司前端人员缺乏的时候,就直接顶上来开发前台页面了(话说我已经很久很久没写java代码了(:′⌒`)) 好吧言归正传,刚开始弄前台就是vue,vue2还没弄熟,老 ...

  3. Asp.Net Core 轻松学-利用日志监视进行服务遥测

    前言     在 Net Core 2.2 中,官方文档表示,对 EventListener 这个日志监视类的内容进行了扩充,同时赋予了跟踪 CoreCLR 事件的权限:通过跟踪 CoreCLR 事件 ...

  4. c# aes,des,md5加密等解密算法

    一:可逆加密,即是能加密也能解密 对称可逆加密:加密后能解密回原文,加密key和解密key是一个 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文 加密解密的速度快, ...

  5. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  6. javascript中Date常用方法

    一.Date的构造函数 有四种形式的Date构造函数: //1.构造函数没有参数,则返回当前日期的Date对象 var now=new Date(); //2.构造函数的参数为日期的毫秒数,返回距离1 ...

  7. 我的Spring Boot学习记录(一):自动配置的大致调用过程

    1. 背景 Spring Boot通过包管理工具引入starter包就可以轻松使用,省去了配置的繁琐工作,这里简要的通过个人的理解说下Spring Boot启动过程中如何去自动加载配置. 本文中使用的 ...

  8. js将一个数组分成多个数组

    1,将数组array分成长度为subGroupLength的小数组并返回新数组 function group(array, subGroupLength) { let index = 0; let n ...

  9. vue项目中获取cdn域名插件

    import axios from 'axios' let CdnPath = {} CdnPath.install = function (Vue, options) { Vue.prototype ...

  10. ios定义数组和字典快捷方式

    //标准写法 NSNumber * number = [NSNumber numberWithInt:1]; NSArray * array = [NSArray arrayWithObjects:@ ...