React Native防止鍵盤遮擋輸入框

防止遮擋鍵盤一般有兩種方法:

1.KeyboardAvoidingView,在最外層加上,設(shè)置offset為150像素(可以根據(jù)情況調(diào)整)。

  <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="請輸入您的手機(jī)號碼"
        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像素可以根據(jù)實際情況做一些修改,你也可以直接設(shè)成鍵盤的高度。

import {
    ......
    ScrollView,  // 引入相關(guān)組件
    Keyboard,
} from 'react-native';


// 監(jiān)聽鍵盤彈出與收回
componentDidMount() {
        this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow',this.keyboardDidShow);
        this.keyboardWillHideListener = Keyboard.addListener('keyboardWillHide',this.keyboardDidHide);
    }

//注銷監(jiān)聽
componentWillUnmount () {
        this.keyboardWillShowListener && this.keyboardWillShowListener.remove();
        this.keyboardWillHideListener && this.keyboardWillHideListener.remove();
    }

//鍵盤彈起后執(zhí)行
keyboardDidShow = () =>  {
        this._scrollView.scrollTo({x:0, y:100, animated:true});
    }

//鍵盤收起后執(zhí)行
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="請輸入您的手機(jī)號碼"
        returnKeyType="done"
        keyboardType="numeric"
        maxLength={13}
        placeholderTextColor={"#666666"}
        />
        </ScrollView>
      </View>
    );
  }
// 監(jiān)聽鍵盤彈出與收回
componentDidMount() {
  this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow',this.keyboardDidShow);
  this.keyboardWillHideListener = Keyboard.addListener('keyboardWillHide',this.keyboardDidHide);
}

//注銷監(jiān)聽
componentWillUnmount () {
  this.keyboardWillShowListener && this.keyboardWillShowListener.remove();
  this.keyboardWillHideListener && this.keyboardWillHideListener.remove();
}

//鍵盤彈起后執(zhí)行
keyboardDidShow = () =>  {
  this._scrollView.scrollTo({x:0, y:100, animated:true});
}

//鍵盤收起后執(zhí)行
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容