设备的键盘与具有文本字段的 ModalBottomSheet 重叠,我已经在清单文件中设置了
android:windowSoftInputMode="adjustResize"
并在底部工作表内容中消耗了 imePadding
。
这是我到目前为止的代码:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val loadProfileModalBottomSheetState = rememberModalBottomSheetState()
val saveProfileModalBottomSheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()
val navController = rememberNavController()
EqualizerTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {...}
) { paddingValues ->
MainNavGraph(navController = navController, paddingValues = paddingValues)
if (loadProfileModalBottomSheetState.isVisible) {
LoadProfilesBottomSheet(
sheetState = loadProfileModalBottomSheetState,
onHide = { scope.launch { loadProfileModalBottomSheetState.hide() } }
)
}
if (saveProfileModalBottomSheetState.isVisible){
SaveProfileBottomSheet(
sheetState = saveProfileModalBottomSheetState,
onHide = { scope.launch { saveProfileModalBottomSheetState.hide() } }
)
}
}
}
}
}
}
@Composable
fun SaveProfileBottomSheet(
modifier: Modifier = Modifier,
sheetState: SheetState,
onHide: () -> Unit,
viewModel: ProfileStorageViewModel = hiltViewModel()
) {
val state = viewModel.state.collectAsState().value
ModalBottomSheet(
modifier = modifier,
sheetState = sheetState,
onDismissRequest = {
onHide()
}
) {
SaveProfileBottomSheetContent(
modifier = Modifier.imePadding(),
profileName = state.profileName,
onProfileNameChange = viewModel::onProfileNameChange,
onHide = {
onHide()
},
onSave = {
onHide()
viewModel.onSave()
}
)
}
}
我解决了这个问题 设置模态底部表单的 fillMaxSize。 然后我使用此方法将模态底部工作表的状态设置为文本字段的Expanded、onValueChange。
suspend fun expandBottomSheet() {
if (bottomSheetState.hasPartiallyExpandedState) {
bottomSheetState.expand()
}
}