kotlin 如何使用jetpack编写导航将可选参数发送到另一个屏幕上的textFields

yhuiod9q  于 2023-04-21  发布在  Kotlin
关注(0)|答案(1)|浏览(134)

我试图用房间数据库和匕首柄制作一个笔记应用程序。一切都很完美,直到我使用撰写中的导航将参数从主屏幕传递到笔记屏幕,用户可以编辑文本并更新它。但由于某种原因,参数没有正确传递到textFields。
我以为问题出在room或hilt上,于是简化了项目,尝试将两个字符串传递给note屏幕上的两个不同的textField,但问题仍然存在。当我将“note”和“content”传递给note屏幕上的两个textField时,第一个textField应该得到“note”,它只显示NavGraph中navArgument的默认值,第二个显示{note_content}notecontent。看起来第二个texField得到了两个参数。这是我的代码。请帮助
主活动和主屏幕

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NavigationTutsProjectTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Navigation()
                }
            }
        }
    }
}

@Composable
fun HomeScreen(navController: NavController){
    val textTitle = "note"
    val textContent = "content"
    Column(
        modifier = Modifier
            .fillMaxSize()
            .clickable {
                navController.navigate(Screen.NoteScreen.route+textTitle+textContent)
            },
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = textTitle)
        Text(text = textContent)
    }
}

带参数的Home和NoteScreen的路由

sealed class Screen(val route: String){
    object HomeScreen: Screen("home_screen")
    object NoteScreen: Screen("note_screen?note_title={note_title}&note_content={note_content}")
}

导航系统

@Composable
fun Navigation(){
    val navController = rememberNavController()
    
    NavHost(navController = navController, startDestination = Screen.HomeScreen.route){
        composable(route = Screen.HomeScreen.route){
            HomeScreen(navController)
        }
        composable(
            route =Screen.NoteScreen.route,
            arguments = listOf(
                navArgument("note_title"){
                    type = NavType.StringType
                    defaultValue = "Nothing"
                },
                navArgument("note_title"){
                    type = NavType.StringType
                    defaultValue = "Nothing"
                }
            )
        ){entry->
            NoteScreen(
                noteTitle = entry.arguments?.getString("note_title"),
                noteContent = entry.arguments?.getString("note_content")
            )
        }
    }
}

注解屏幕

@Composable
fun NoteScreen(
    noteTitle: String?,
    noteContent: String?
){
    var textFieldTitle by remember {
        mutableStateOf(noteTitle)
    }
    var textFieldContent by remember {
        mutableStateOf(noteContent)
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        textFieldTitle?.let {
            TextField(
                value = it, onValueChange = {it2->
                    textFieldTitle = it2
                }
            )
        }
        textFieldContent?.let {
            TextField(
                value = it, onValueChange = {it2->
                    textFieldContent = it2
                }
            )
        }
    }
}

这是我运行代码时得到的结果
enter image description here

vsikbqxv

vsikbqxv1#

试试看

Screen.NoteScreen.route.replace("{note_title}",textTitle).replace("{note_content}" ,textContent)

而不是

Screen.NoteScreen.route+textTitle+textContent

相关问题