【发布时间】:2020-01-07 05:10:48
【问题描述】:
您好,我制作了一个应用程序并尝试在应用程序的两个片段之间传递数据
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/title2">
<fragment
android:id="@+id/title2"
android:name="com.example.order10.title"
android:label="title" >
<action
android:id="@+id/action_title2_to_pageone"
app:destination="@id/pageone" />
</fragment>
<fragment
android:id="@+id/pageone"
android:name="com.example.order10.pageone"
android:label="pageone" >
<action
android:id="@+id/action_pageone_to_finalpage"
app:destination="@id/finalpage" >
<argument android:defaultValue="Hi"
android:name="text" />
</action>
</fragment>
<fragment
android:id="@+id/finalpage"
android:name="com.example.order10.finalpage"
android:label="fragment_finalpage"
tools:layout="@layout/fragment_finalpage" >
<argument
android:name="order_text"
app:argType="string"
android:defaultValue="Hi" />
</fragment>
<fragment
android:id="@+id/about"
android:name="com.example.order10.about"
android:label="Order 1.0" />
</navigation>
数据传递代码
fun navigation(text:String){
var args= bundleOf()
args.putString("order_text",text)
view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
} navigation("This is order one ")
接收数据的代码
val order_text:TextView?=view?.findViewById(R.id.order_text)
order_text?.text=arguments?.getString("order_text")
但是它不工作它甚至不显示默认文本,我已经应用了所有插件和依赖项
【问题讨论】:
-
点击后能显示代码吗?
-
以下答案不起作用,请有人提供更好的方法
-
请分享您的错误或解释更多!
-
嘿,我正在应用下面的两个答案,但是当我运行时,我只是在导航,甚至没有显示默认文本。如果您不介意,您能否请一个两屏应用程序,其中数据从片段 1 传递到片段 2,并将其上传到 GitHub 之类的地方,以便我可以比较它。顺便说一句没有编译错误
-
@AshutoshPanda,我更新我的答案,使用它
标签: android android-fragments kotlin navigation