Kotlin Jetpack ComposeView小屏设备键盘遮挡OutlinedTextField问题
解决Jetpack Compose中小屏幕设备键盘遮挡OutlinedTextField的问题
在Nokia 6.1(型号TA-1089,Android 10系统)这类小屏幕设备上,使用Jetpack Compose实现的OutlinedTextField会被弹出的键盘遮挡,以下是问题相关代码及解决方案:
问题代码
Compose中OutlinedTextField实现
value = passwordText.value!!, onValueChange = { passwordText.value = it enableLogin = studentIDText.value.isNullOrEmpty() .not() && passwordText.value.isNullOrEmpty() .not() }, modifier = Modifier .fillMaxWidth() .padding(horizontal = 8.dp) .border( border = BorderStroke( 1.dp, colorResource(id = R.color.light_white) ), shape = RoundedCornerShape(5.dp) ), visualTransformation = PasswordVisualTransformation(), keyboardOptions = KeyboardOptions( keyboardType = KeyboardType.Password, imeAction = ImeAction.Done ), keyboardActions = KeyboardActions( onDone = { focusManager.clearFocus() }), placeholder = { SBText( text = stringResource(id = R.string.enter_password), size = 16.sp, color = colorResource(id = R.color.light_white), font = SBFont.WorksansRegular, lineHeight = 16.sp, ) }, colors = TextFieldDefaults.outlinedTextFieldColors( textColor = colorResource(id = R.color.grey), cursorColor = colorResource(id = R.color.grey), focusedBorderColor = colorResource(id = R.color.light_white), unfocusedBorderColor = colorResource(id = R.color.light_white) ), maxLines = 1, singleLine = true
AndroidManifest.xml配置
android:name=".ui.SBActivity" android:configChanges="orientation|screenSize" android:screenOrientation="portrait" android:theme="@style/Theme.SB.NoActionBar" android:showWhenLocked="false" android:exported="true" android:windowSoftInputMode="stateAlwaysHidden|adjustResize|adjustPan" android:taskAffinity="">
解决方案
修正WindowSoftInputMode配置
同时设置adjustResize和adjustPan会导致系统行为冲突,移除其中一个,推荐保留adjustResize让布局自动适应键盘高度:android:windowSoftInputMode="stateAlwaysHidden|adjustResize"使用可滚动容器包裹布局
将包含输入框的外层布局改为可滚动容器,确保键盘弹出时可以滚动到输入框:Column( modifier = Modifier .fillMaxSize() .verticalScroll(rememberScrollState()) .padding(vertical = 16.dp) ) { // 页面其他元素 // 你的OutlinedTextField代码 }检查主题配置
确保Activity主题未设置全屏属性,且开启fitsSystemWindows,避免阻止布局调整:
在主题样式中添加或修改:<item name="android:fitsSystemWindows">true</item> <!-- 移除android:windowFullscreen属性(如果存在) -->
内容的提问来源于stack exchange,提问作者nikita soloman
相关产品推荐
相关产品推荐

