pru*_*ddy 14 android android-navigation android-jetpack-compose
https://developer.android.com/jetpack/compose/navigation#optional-args
一直在查看文档以了解如何使用多个可选参数以及如何传递它们
但在文档中只提到了一个参数。
composable(
"profile?userId={userId}",
arguments = listOf(navArgument("userId") { defaultValue = "me" })
)
Run Code Online (Sandbox Code Playgroud)
并调用它
composable("profile")
composable("profile/user123") // if you want to pass param
Run Code Online (Sandbox Code Playgroud)
如何声明和调用两个参数?
pru*_*ddy 49
如何申报?
composable(
"profile?userId={userId}&userType={userType}",
arguments = listOf(
navArgument("userType") {
defaultValue = "ADMIN"
type = NavType.StringType
}, navArgument("userId") {
nullable = true
defaultValue = null
type = NavType.StringType
})
)
Run Code Online (Sandbox Code Playgroud)
怎么打电话?
navController.navigate("profile?userId=user123&userType=user")
navController.navigate("profile?userType=user")
navController.navigate("profile")
Run Code Online (Sandbox Code Playgroud)
这是使用多个可选参数的详细答案
// Syntax to declare
NavHost(...) {
composable(
"profile?userId={userId}&username={username}&address={address}",
arguments = listOf(
navArgument("userId") {
type = NavType.IntType
defaultValue = 86
}
navArgument("username") {
type = NavType.StringType
defaultValue = "rahul2211"
}
navArgument("address") {
type = NavType.StringType
defaultValue = "India"
}
)
) { backStackEntry ->
val userId = backStackEntry.arguments?.getInt("userId")
val username = backStackEntry.arguments?.getString("username")
val address = backStackEntry.arguments?.getString("address")
Profile(navController, userId, username, address)
}
}
Run Code Online (Sandbox Code Playgroud)
这是您可以选择将参数传递给 Profile() 可组合项的方式
// Syntax to call
navController.navigate("profile?userId=102&username=shrekssid&address=Germany")
// userId = 102, username = "shrekssid", address = "Germany"
navController.navigate("profile?address=Italy")
// userId = 86, username = "rahul2211", address = "Italy"
navController.navigate("profile?userId=991&address=Germany")
// userId = 991, username = "rahul2211", address = "Germany"
navController.navigate("profile")
// userId = 86, username = "rahul2211", address = "India"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11231 次 |
| 最近记录: |