应用程序的结构相当简单:底部有一个搜索栏、一个列表视图和react-native-tabs。问题:如果我点击 Android 上的搜索栏,它会将整个应用程序向上推,因此我可以直接在键盘上看到选项卡。但在 iOS 上,键盘覆盖了整个应用程序,不会向上推任何东西。有什么办法可以控制吗
如果问题不清楚,我很乐意分享代码/屏幕截图。
谢谢
编辑:
<View style={{flex:1, backgroundColor:'#f2f2f2'}}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderSearchResults.bind(this)}
style={styles.listView}/>
<KeyboardAvoidingView
style={styles.addQuestionBar}
behavior={'position'}>
<Text>
Don't see your question?
</Text>
<TouchableOpacity>
<Text>
Add it
</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
</View>
在清单文件中设置
android:windowSoftInputMode="adjustPan"
,它将按您的预期工作。
例如
<application
android:name=".MainApplication"
android:allowBackup="true"
...
<activity
android:name=".MainActivity"
android:label="@string/app_name"
...
android:windowSoftInputMode="adjustPan">
...
</activity>
...
</application>
对于那些使用Expo的人
@J KW 的答案是正确的,但如果您使用 Expo,则必须以不同的方式实现它。
Expo 使用不同的配置术语。在你的 app.json 中你必须设置配置键
"softwareKeyboardLayoutMode":"pan"
在你的 android 对象中。
您的文件可能类似于:
{
"expo": {
"name": "App",
...
"android":{"softwareKeyboardLayoutMode": "pan"},
...
}
}
注意:如果您收到“不应有附加属性”错误,可能是因为您没有更新的 Expo SDK (v.038)。请更新您的 Expo 版本。
React Native 中有一个名为 KeyboardAvoidingView 的新组件尚未记录,但我已经在我的项目中使用它并且它非常有用
代码示例:
'use strict'
import { ... , KeyboardAvoidingView } from 'react-native'
class Test extends Component {
constructor () {
super()
this.state = {
behavior: 'position'
// there is three ways to adjust (position , height , padding )
}
}
render(){
return (
<View>
<KeyboardAvoidingView behavior={this.state.behavior}>
<TextInput
style={style.PhoneNumberInput}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
</KeyboardAvoidingView>
</View>
)
}
}
module.exports = Test
有关更多详细信息,您可以查看 KeyboardAvoidingViewExample
编辑:
抱歉,我刚刚从这个问题中得到了错误的想法,我认为您想要做的是阻止 Android 键盘将应用程序向上推,这里是允许您在 Android 中选择(平移、调整大小、无)的组件
android:windowSoftInputMode="adjustResize".
android:windowSoftInputMode
中为
AndroidManifest.xml
设置了“adjustPan”,KeyboardAvoidingView 和其他键盘相关组件将无法正常工作。
相反,你应该使用“adjustResize”并拥有美好的生活。
我有“adjustPan”并尝试使用KeyboardAvoidingView和KeyboardAwareScrollView...没有任何效果。
现在,通过“调整调整大小”,我没有使用任何与键盘相关的组件,并且我的 Android 应用程序可以正常工作。 (我可能必须在 iOS 上使用 KeyboardAvoiding 视图,但它可以开箱即用。)
@binkie 的答案适用于我的 expo(版本 44.0.0)应用程序,但略有变化。 在 app.json 中,
"android":{"softwareKeyboardLayoutMode": "pan"}
在屏幕中,边距底部值等于底部选项卡的高度,如下所示
<ScrollView mb="70px">...</ScrollView>
****1)修改AndroidMainfest.xml中 android/src/main/AndroidMainfest.xml 您可以通过更改来解决问题
$ android:windowSoftInputMode="adjustResize";
to
$ android:windowSoftInputMode="adjustPan";
the problem will be resolvede****
我在 React Native - Javascript 时遇到了同样的问题,
SafeAreaView
和 ScrollView
解决了问题
<SafeAreaView style={styles.container}>
<ScrollView >
<View style={styles.body}>
....
container: {
flex: 1,
backgroundColor: 'white'
},
根据这里(以及类似主题)的许多答案,react-native Github问题和媒体帖子,该问题不能只有一种解决方案/方法(我数了至少4个)。顺便说一句,这个线程中没有人对我的情况有帮助。但手动隐藏就完成了这项工作。
我在一些 RN/AwareScrollView 的默认行为无法按预期工作的地方使用这个钩子。
export default function useIsKeyboardShown() {
const [isShown, setIsShown] = useState(false);
useEffect(() => {
const keyboardDidShowSubscription =
Keyboard.addListener('keyboardDidShow', () => {
setIsShown(true);
});
const keyboardDidHideSubscription =
Keyboard.addListener('keyboardDidHide', () => {
setIsShown(false);
});
return () => {
keyboardDidShowSubscription?.remove();
keyboardDidHideSubscription?.remove();
};
}, []);
return isShown;
}
像这样例如:
const isKeyboardShown = useIsKeyboardShown();
<View
style={[styles.outerBtnContainer, isKeyboardShown ? { height: 0 } : {}]}
></View>