react native输入框定位在底部(虚拟键盘弹起)
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输入框定位在底部(虚拟键盘弹起)的更多相关文章
- React Native(四)——顶部以及底部导航栏实现方式
效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...
- Swift - 点击输入框外部屏幕关闭虚拟键盘
我们如果把文本框的Return Key设置成Done,然后在storyboard中将文本框的Did End On Exit事件在代码里进行关联.同时关联代码里调用文本框的resignFirstResp ...
- React Native 的组件之底部导航栏 TabBarIOS(一)
import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, TabBarIOS, } fro ...
- apicloud iphoneX底部虚拟键盘遮挡
1.首先,底部的高不能写死. 2. var footer = $api.byId('footer'); $api.fixTabBar(footer);这句应该写在 footerHeight = $ap ...
- 移动端fixed定位在底部,出现键盘后消失
jq var h=$(window).height(); $(window).resize(function() { if($(window).height()<h){ $('.nav').hi ...
- Android适配底部返回键等虚拟键盘的完美解决方案
这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...
- Android 适配底部返回键等虚拟键盘的完美解决方案
这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...
- 虚拟键盘,移动web开发的痛
原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗? 你可能会说着:“不就是放一个position: fixed;的输入框在 ...
- React Native 从入门到原理
React Native 是最近非常火的一个话题,介绍如何利用 React Native 进行开发的文章和书籍多如牛毛,但面向入门水平并介绍它工作原理的文章却寥寥无几. 本文分为两个部分:上半部分用通 ...
随机推荐
- Asp.Net.Identity认证不依赖Entity Framework实现方式
Asp.Net.Identity为何物请自行搜索,也可转向此文章http://www.cnblogs.com/shanyou/p/3918178.html 本来微软已经帮我们将授权.认证以及数据库存储 ...
- 第三节:dingo/API 最新版 V2.0 之 Creating API Endpoints (连载)
对于这篇文章的标题,其实,直译就是创建api端点.但是,真的很难懂,我还是写为API路由吧.每篇,文章,我都会全部去进行实践操作,力求写一个好点的教程. 本文英文地址——>https://git ...
- 引用KBC.PetroSIM.Interop的dll,在代码中调用时出现 80040154 没有注册类 的错误
失败的尝试: regsvr32注册:模块已加载,但找不到入口点DllRegisterServer regasm注册:需写上regasm的完整路径,注册成功,但问题依旧 将项目的平台改为x86:问题依旧 ...
- [转]Date and String Function in BluePrism
本文转自:https://www.codeproject.com/Articles/1247389/Date-and-String-Function-in-BluePrism This article ...
- MySQL 笔记整理(18) --为什么这些SQL语句逻辑相同,性能却差异巨大?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 18) --为什么这些SQL语句逻辑相同,性能却差异巨大? 本篇我们以三 ...
- Raptor井字棋游戏
作为大学第一个小作品,记录一下,也给那些想接触到Raptor游戏的人一个小小的参考QAQ至于Raptor的语法和使用,可以参考一下他的帮助手册,看不懂英文的话可以复制放到翻译上看. 以上是主函数 以下 ...
- Gulp 前端优化
使用方法: 下载 node.js , https://nodejs.org/en/,并安装 msi 一下命令都属于 dos 命令 node -v,npm -v,检验是否下载成功(出现版本号) 将 np ...
- python turtle 书写新年快乐
文章链接:https://mp.weixin.qq.com/s/xYSKH_KLYfooIoelJH02Cg 农历2018年的最后一篇文章,踏上回家的征途前,推荐一个Python的三方库turtle, ...
- Android 跳转权限设置界面的终极方案
转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/10338377.html 有时候APP需要获取某些权限,要让用户手动打开,那么问题来了,若是直接提醒用户出 ...
- base64图片存储
将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方. Base64编码在ora ...