在kotlin中...我想将数据传递到扩展了dialogFragment的自定义弹出窗口,所以这里的任何人都知道如何将数据传递给这样的片段?
每次将数据传递给构造函数时,我都会出错。
请帮忙。
将数据传递给构造函数
class PopUpClass : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var v = inflater.inflate(R.layout.poplayout,container,false
return v
}
//tried to pass the data in the constructor and then handle it but did not work
最佳答案
这是您在Kotlin中的操作方法,首先使用片段类的实例声明一个伴随对象
companion object {
@JvmStatic //This can be avoided if you are in a complete Kotlin project
fun newInstance(content: String): PopUpClass {
val args = Bundle()
args.putString("content", content)
val fragment = PopUpClass()
fragment.arguments = args
return fragment
}
}
在片段的onCreate()或onViewCreated()中,您可以像这样接收数据
val dataPassed: String? = arguments?.getString("content")
从父 Activity 或片段中调用newInstance而不是构造函数
关于kotlin - 我想将数据传递给弹出对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55883517/