我有一个 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
}
}
这是我想调用的函数。对话 从那里。
private val fm = supportFragmentManager
fun TestFunction() {
Dialog().show(fm, "")
}
但是 supportFragmentManager 是红色的,在函数中不被识别。
supportFragmentManager
是变量 Activity
或 fragment
类,所以你不能不在Activity类或片段中使用。