如何避免在本机反应中打开键盘时屏幕崩溃
How to avoid collapsing of screen when keyboard opens in react native
在我的应用程序中,着陆页是登录屏幕。我使用 native-base
ui kit.The 完成了它,登录屏幕包含一个 logo
并且下方将有 2 个用户名和密码输入字段以及登录按钮。问题是,每当我单击输入字段时,login form
都会在徽标下方折叠。我会为它分享相应的屏幕。我尝试了以下方法
android:windowSoftInputMode="adjustResize"
在 AndroidManifest.xml
中的 activity
标签内。但仍然得到相同的屏幕。请看一下屏幕。
- 正常屏幕行为https://i.stack.imgur.com/ZFqfI.png
- 键盘打开后https://i.stack.imgur.com/tZr1i.png
我也试过了KeyboardAvoidngView
代码如下
render() {
return (
<KeyboardAvoidingView behavior="padding" style={{flex:1}}>
<Container>
<StatusBar barStyle="light-content" />
<View style={styles.logoContainer}>
<ImageBackground source={launchscreenLogo} style={styles.logo} />
</View>
<View
style={{
flex:1,
marginBottom: 200,
backgroundColor: "transparent"
}}
>
<Form style={{margin:10}}>
<Item style={{margin:10}}>
<Icon active name='person' />
<Input
placeholder='Username'
style={{marginLeft:18, color:'#000'}}
value={this.state.username}
onChangeText={username => this.setState({ username })}
/>
</Item>
<Item style={{margin:10}} >
<Icon active name='mail' />
<Input
placeholder='Email'
style={{marginLeft:18, color:'#000'}}
value={this.state.email}
onChangeText={email => this.setState({email})}/>
</Item>
<Item style={{margin:10}}>
<Icon active name='ios-lock' />
<Input
placeholder='Password' style={{marginLeft:18, color:'#000'}}
value={this.state.password}
onChangeText={password => this.setState({ password })}
/>
</Item>
<Item style={{margin:10}}>
<Icon active name='ios-lock' />
<Input
placeholder='Repeat Password'
style={{marginLeft:18, color:'#000'}}
value={this.state.r_password}
onChangeText={r_password => this.setState({ r_password })}/>
</Item>
</Form>
<Button
style={{ backgroundColor: "#809fff", alignSelf: "center", elevation:20}}
onPress={() => this.props.navigation.navigate("Anatomy")}
>
<Text style={{fontWeight:'bold',fontSize:18}}>REGISTER</Text>
</Button>
</View>
<View style={{ marginBottom: 80 }}>
</View>
</Container>
</KeyboardAvoidingView>
);
}
您可以使用 KeyboardAwareScrollView
.
添加使用npm i react-native-keyboard-aware-scroll-view --save
然后使用它
<KeyboardAwareScrollView
keyboardShouldPersistTaps= 'always'
style= {{ flex:1 }}
>
<View>
<TextInput />
</View>
</KeyboardAwareScrollView>
有关详细信息,请参阅 this。希望这有帮助
在我的应用程序中,着陆页是登录屏幕。我使用 native-base
ui kit.The 完成了它,登录屏幕包含一个 logo
并且下方将有 2 个用户名和密码输入字段以及登录按钮。问题是,每当我单击输入字段时,login form
都会在徽标下方折叠。我会为它分享相应的屏幕。我尝试了以下方法
android:windowSoftInputMode="adjustResize"
在 AndroidManifest.xml
中的 activity
标签内。但仍然得到相同的屏幕。请看一下屏幕。
- 正常屏幕行为https://i.stack.imgur.com/ZFqfI.png
- 键盘打开后https://i.stack.imgur.com/tZr1i.png
我也试过了KeyboardAvoidngView
代码如下
render() {
return (
<KeyboardAvoidingView behavior="padding" style={{flex:1}}>
<Container>
<StatusBar barStyle="light-content" />
<View style={styles.logoContainer}>
<ImageBackground source={launchscreenLogo} style={styles.logo} />
</View>
<View
style={{
flex:1,
marginBottom: 200,
backgroundColor: "transparent"
}}
>
<Form style={{margin:10}}>
<Item style={{margin:10}}>
<Icon active name='person' />
<Input
placeholder='Username'
style={{marginLeft:18, color:'#000'}}
value={this.state.username}
onChangeText={username => this.setState({ username })}
/>
</Item>
<Item style={{margin:10}} >
<Icon active name='mail' />
<Input
placeholder='Email'
style={{marginLeft:18, color:'#000'}}
value={this.state.email}
onChangeText={email => this.setState({email})}/>
</Item>
<Item style={{margin:10}}>
<Icon active name='ios-lock' />
<Input
placeholder='Password' style={{marginLeft:18, color:'#000'}}
value={this.state.password}
onChangeText={password => this.setState({ password })}
/>
</Item>
<Item style={{margin:10}}>
<Icon active name='ios-lock' />
<Input
placeholder='Repeat Password'
style={{marginLeft:18, color:'#000'}}
value={this.state.r_password}
onChangeText={r_password => this.setState({ r_password })}/>
</Item>
</Form>
<Button
style={{ backgroundColor: "#809fff", alignSelf: "center", elevation:20}}
onPress={() => this.props.navigation.navigate("Anatomy")}
>
<Text style={{fontWeight:'bold',fontSize:18}}>REGISTER</Text>
</Button>
</View>
<View style={{ marginBottom: 80 }}>
</View>
</Container>
</KeyboardAvoidingView>
);
}
您可以使用 KeyboardAwareScrollView
.
添加使用npm i react-native-keyboard-aware-scroll-view --save
然后使用它
<KeyboardAwareScrollView
keyboardShouldPersistTaps= 'always'
style= {{ flex:1 }}
>
<View>
<TextInput />
</View>
</KeyboardAwareScrollView>
有关详细信息,请参阅 this。希望这有帮助