我正在按照 Now In Android 中的示例编写一个应用程序。
在
Scaffold
中,我有一个浮动操作按钮,我想将其 onClick
事件传递到另一个屏幕,这样我就可以从该屏幕获取内容并将其传递到文本转语音 (TTS) 阅读器将显示在 ModalBottomSheet
。
我的问题是我不知道如何传递该事件以在另一个屏幕上捕获它。
这是我加载
Scaffold
的代码:
@Composable
fun LPlusApp(appState: LPlusAppState, onReaderClick: () -> Unit) {
LPlusBackground {
LPlusGradientBackground(/* code */) {
val snackbarHostState = remember { SnackbarHostState() }
val isOffline by appState.isOffline.collectAsStateWithLifecycle()
val notConnectedMessage = stringResource(R.string.not_connected)
LaunchedEffect(isOffline) {
//...
}
// ...
Scaffold(
modifier = Modifier.semantics {
testTagsAsResourceId = true
},
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onBackground,
contentWindowInsets = WindowInsets(0, 0, 0, 0),
snackbarHost = { SnackbarHost(snackbarHostState) },
floatingActionButton =
{
SmallFloatingActionButton(onClick = onReaderClick)
{ Icon(Icons.Filled.PlayArrow, contentDescription = "") }
},
) { padding ->
// ...
}
}
}
}
这是我设置内容的
MainActivity
代码:
setContent {
val darkTheme = shouldUseDarkTheme(uiState)
DisposableEffect(darkTheme) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
android.graphics.Color.TRANSPARENT,
android.graphics.Color.TRANSPARENT,
) { darkTheme },
navigationBarStyle = SystemBarStyle.auto(
lightScrim,
darkScrim,
) { darkTheme },
)
onDispose {}
}
val appState = rememberLPlusAppState(
windowSizeClass = calculateWindowSizeClass(this),
networkMonitor = networkMonitor,
universalisRepository = universalisRepository,
timeZoneMonitor = timeZoneMonitor,
)
val currentTimeZone by appState.currentTimeZone.collectAsStateWithLifecycle()
CompositionLocalProvider(
LocalAnalyticsHelper provides analyticsHelper,
LocalTimeZone provides currentTimeZone,
) {
LPlusTheme(
darkTheme = darkTheme,
androidTheme = shouldUseAndroidTheme(uiState),
disableDynamicTheming = shouldDisableDynamicTheming(uiState),
) {
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
LPlusApp(appState,{})
}
}
}
这就是我想要捕获
onReaderClick
事件的路线和屏幕:
@ExperimentalMaterial3AdaptiveApi
@Composable
fun UniversalisRoute(
onBackClick: () -> Unit,
modifier: Modifier = Modifier,
viewModel: UniversalisViewModel = hiltViewModel(),
onReaderClick: () -> Unit,
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
UniversalisScreen(
uiState = uiState,
modifier = modifier,
onReaderClick = {
Timber.d("Launch ModalBottomSheet with TTS")
},
onBackClick = onBackClick,
)
}
代码对我不起作用,我应该如何捕获
UniversalisScreen
中浮动操作按钮的onClick事件?
最简单的方法不是使用 Intents 和 Intents 接收器吗?喜欢:
context.registerReceiver(
reciverFunction,
IntentFilter("YourActivity.INTENT_NAME"),
RECEIVER_NOT_EXPORTED
)
并且:
intent=Intent("YourActivity.INTENT_NAME")
intent.putExtra("EXTRA_NAME", "DATA")
startActivity(intent)
希望这有帮助!
我没有犯任何错误