使用导航中的TypeSafe将多个参数传递给片段

问题描述 投票:0回答:1

我知道可以使用源片段中的Action传递单个参数

  override fun onClick(v: View) {
       val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
       val amount = amountTv.text.toString().toInt()
       val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
       v.findNavController().navigate(action)

}

并按照android docs中的指定在目标片段中获取它

    val args: ConfirmationFragmentArgs by navArgs()
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            val tv: TextView = view.findViewById(R.id.textViewAmount)
            val amount = args.amount
            tv.text = amount.toString()
}

请让我知道有什么方法可以通过TypeSafe方式传递多个参数

android android-architecture-components android-jetpack android-navigation
1个回答
0
投票

是,您可以通过在导航图中为片段定义多个参数,然后将它们传递给代码中的action来实现。这是一个例子:

© www.soinside.com 2019 - 2024. All rights reserved.