Jetpack Compose 탐색 생성 및 인수 제출
18558 단어 kotlinjetpackcomposeandroidtutorial
그럼 시작하겠습니다.
샘플 응용 프로그램: https://github.com/togisoft/JetpackNavigationExample
sealed class Screen(val route: String) {
object HomeScreen : Screen(route ="home")
object DetailScreen : Screen(route = "detail")
// To send multiple arguments
fun withArgs(vararg args: String): String {
return buildString {
append(route)
args.forEach { arg ->
append("/$arg")
}
}
}
}
@Composable
fun HomeScreen(navController: NavController) {
var name by remember { mutableStateOf("") }
var surname by remember { mutableStateOf("") }
val context = LocalContext.current
Scaffold(
topBar = {
TopAppBar(title = { Text(text = "Home Screen") })
}
) {
Box(modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = name,
label = { Text(text = "Name") },
onValueChange = { name = it },
modifier = Modifier.padding(vertical = 16.dp)
)
OutlinedTextField(
value = surname,
label = { Text(text = "Surname") },
onValueChange = { surname = it },
modifier = Modifier.padding(vertical = 16.dp)
)
Button(onClick = {
// Check if textfields are null
if (name.isNotEmpty()) {
//With route arguments for Detail Screen. navController.navigate(Screen.DetailScreen.withArgs(name, surname))
} else {
Toast.makeText(context, "Fill in the required fields.", Toast.LENGTH_SHORT)
.show()
}
}) {
Text(text = "Send Detail Screen")
}
}
}
}
}
@Composable
fun DetailScreen(name: String?, surname: String?, navController: NavController) {
Scaffold(
topBar = {
TopAppBar(title = { Text(text = "Detail Screen") }, navigationIcon = {
IconButton(
onClick = {
navController.popBackStack()
}) {
Icon(imageVector = Icons.Rounded.ArrowBack, contentDescription = "posStackBack")
}
})
}
) {
Box(modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Hello $name $surname", fontWeight = FontWeight.Bold)
}
}
}
}
@Composable
fun Navigation() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Screen.HomeScreen.route) {
composable(route = Screen.HomeScreen.route) {
HomeScreen(navController = navController)
}
composable(route = Screen.DetailScreen.route + "/{name}/{surname}",
arguments = listOf(
navArgument("name") {
type = NavType.StringType
nullable = false
},
navArgument("surname") {
type = NavType.StringType
nullable = false
}
)
) { entry ->
DetailScreen(
name = entry.arguments?.getString("name"),
surname = entry.arguments?.getString("surname"),
navController = navController
)
}
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DevToTheme {
Navigation()
}
}
}
}
*** 최종**
Reference
이 문제에 관하여(Jetpack Compose 탐색 생성 및 인수 제출), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/caglayantolga/creating-jetpack-compose-navigation-and-submitting-arguments-37b6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)