【发布时间】:2020-09-25 12:02:22
【问题描述】:
我有一个名为 Dialog 的 DialogFragment。如何从 Android 中没有活动和片段的函数调用它?
这是DialogFragment:
class Dialog: DialogFragment() {
private var array = arrayOf("Yes", "No")
var a = ""
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.activity_main, container)
val myListView = rootView.findViewById(R.id.listview_1) as ListView
myListView.adapter = ArrayAdapter(context!!, R.layout.list_item, array)
myListView.setItemChecked(0,true)
val okbutton = rootView.findViewById<Button>(R.id.ok)
var cancelbutton = rootView.findViewById<Button>(R.id.cancel)
var title = rootView.findViewById<TextView>(R.id.title)
title.text="Choose one option"
cancelbutton.setOnClickListener { dismiss() }
okbutton.setOnClickListener {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show()
}
myListView.setOnItemClickListener { adapterView,
view,
position,
l
->
Toast.makeText(context, "${array[position]}", Toast.LENGTH_LONG).show()
}
return rootView
}
}
这是我想从那里调用 Dialog 的函数:
private val fm = supportFragmentManager
fun TestFunction() {
Dialog().show(fm, "")
}
但是supportFragmentManager是红色的,在函数中无法识别。
【问题讨论】:
-
那么通过参数发送片段管理器呢?
-
怎么样?请发送答案
-
我认为您不能在片段或活动之外显示对话框。
标签: android function android-studio dialogfragment