美文网首页React Native学习
React Native防止键盘遮挡输入框

React Native防止键盘遮挡输入框

作者: 一亩三分甜 | 来源:发表于2018-09-24 19:29 被阅读7次

防止遮挡键盘一般有两种方法:

1.KeyboardAvoidingView,在最外层加上,设置offset为150像素(可以根据情况调整)。

  <KeyboardAvoidingView 
      behavior="padding"
      keyboardVerticalOffset={150}
      >
 </KeyboardAvoidingView>

使用中代码如下:

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  TextInput,
  KeyboardAvoidingView
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

export default class App extends Component<{}> {
  render() {
    return (
      <View style={styles.container}>
      <KeyboardAvoidingView 
      behavior="padding"
      keyboardVerticalOffset={150}
      >
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TextInput
        style={{width:'100%',marginTop:300,borderBottomWidth:1,borderStyle:'solid',textAlign:'left'}}
        placeholder="请输入您的手机号码"
        returnKeyType="done"
        keyboardType="numeric"
        maxLength={13}
        placeholderTextColor={"#666666"}
        />
        </KeyboardAvoidingView>
      </View>
    );
  }
}

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,
  },
});
1.gif

2.ScrollView,用ScrollView将TextInput等组件包一层:这样基本就将键盘遮挡问题很好的解决了,位移100像素可以根据实际情况做一些修改,你也可以直接设成键盘的高度。

import {
    ......
    ScrollView,  // 引入相关组件
    Keyboard,
} from 'react-native';


// 监听键盘弹出与收回
componentDidMount() {
        this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow',this.keyboardDidShow);
        this.keyboardWillHideListener = Keyboard.addListener('keyboardWillHide',this.keyboardDidHide);
    }

//注销监听
componentWillUnmount () {
        this.keyboardWillShowListener && this.keyboardWillShowListener.remove();
        this.keyboardWillHideListener && this.keyboardWillHideListener.remove();
    }

//键盘弹起后执行
keyboardDidShow = () =>  {
        this._scrollView.scrollTo({x:0, y:100, animated:true});
    }

//键盘收起后执行
keyboardDidHide = () => {
        this._scrollView.scrollTo({x:0, y:0, animated:true});
    }
    <ScrollView ref={component => this._scrollView=component} scrollEnabled={false}
                        keyboardShouldPersistTaps={true}>
      ...... // 其他组件代码

</ScrollView>

使用中代码如下:

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  TextInput,
  KeyboardAvoidingView,
  ScrollView,
  Keyboard
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

export default class App extends Component<{}> {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <ScrollView ref={component => this._scrollView=component} scrollEnabled={false}
                        keyboardShouldPersistTaps={true}>
        <TextInput
        style={{width:'100%',marginTop:500,borderBottomWidth:1,borderStyle:'solid',textAlign:'left'}}
        placeholder="请输入您的手机号码"
        returnKeyType="done"
        keyboardType="numeric"
        maxLength={13}
        placeholderTextColor={"#666666"}
        />
        </ScrollView>
      </View>
    );
  }
// 监听键盘弹出与收回
componentDidMount() {
  this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow',this.keyboardDidShow);
  this.keyboardWillHideListener = Keyboard.addListener('keyboardWillHide',this.keyboardDidHide);
}

//注销监听
componentWillUnmount () {
  this.keyboardWillShowListener && this.keyboardWillShowListener.remove();
  this.keyboardWillHideListener && this.keyboardWillHideListener.remove();
}

//键盘弹起后执行
keyboardDidShow = () =>  {
  this._scrollView.scrollTo({x:0, y:100, animated:true});
}

//键盘收起后执行
keyboardDidHide = () => {
  this._scrollView.scrollTo({x:0, y:0, animated:true});
}
}
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,
  },
});
2.gif

相关文章

网友评论

    本文标题:React Native防止键盘遮挡输入框

    本文链接:https://www.haomeiwen.com/subject/mhjhoftx.html