防止遮擋鍵盤一般有兩種方法:
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