我在 kotlin 片段中有一個自定義對話框,其中有 2 個按鈕和一個進度條submitSpk()
class RincianPembelianFragment : androidx.fragment.app.Fragment(), BlockingStep {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_spk_rincian_bayar, container, false)
//initialize your UI
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.supportFragmentManager?.popBackStack(null, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE)
setContext(activity!!.applicationContext)
}
fun submitSpks (){
val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val builder = AlertDialog.Builder(context!!)
val dialogView = inflater.inflate(R.layout.activity_signature, null)
builder.setView(dialogView)
builder.setTitle("Tanda Tangan Pembeli")
//builder.show()
builder.setCancelable(false)
dialogView.clear.setOnClickListener {
dialogView.signature_view.clearCanvas()
}
dialogView.save.setOnClickListener {
submitTtd() // or some fuction else
dialogView.save.visibility = View.INVISIBLE
dialogView.progressBar.visibility = View.VISIBLE
}
builder.setNegativeButton(""){ dialog: DialogInterface?, which: Int ->
}
builder.show()
}
fun submitTtd(){
// here there will be a crud transaction and visible/invisible button save
}
}
我的自定義對話框是這樣的
我想dialogView.save.visibility = View.VISIBLE從另一個功能中看到按鈕保存的可見性,例如saveTtd()
我該怎么做?我已經嘗試過了,但我的對話框無法顯示.. 任何人都可以幫助我嗎?之前謝謝
uj5u.com熱心網友回復:
將 存盤dialogView在全域變數中應該可以完成這項作業。
此外,在顯示對話框之前呼叫實體build()上的方法:AlertDialog.Builder
class RincianPembelianFragment : androidx.fragment.app.Fragment(), BlockingStep {
private lateinit var dialogView: View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_spk_rincian_bayar, container, false)
//initialize your dialog view
dialogView = inflater.inflate(R.layout.activity_signature, null)
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.supportFragmentManager?.popBackStack(null, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE)
setContext(activity!!.applicationContext)
}
fun submitSpks() {
// Using requireContext() is suggested
val builder = AlertDialog.Builder(requireContext())
builder.setView(dialogView)
builder.setTitle("Tanda Tangan Pembeli")
builder.setCancelable(false)
dialogView.clear.setOnClickListener {
dialogView.signature_view.clearCanvas()
}
dialogView.save.setOnClickListener {
submitTtd() // or some fuction else
}
builder.setNegativeButton(""){ dialog: DialogInterface?, which: Int ->
}
// Build the dialog builder before showing it
val dialog = builder.build()
dialog.show()
}
fun submitTtd(){
// here there will be a crud transaction and visible/invisible button save
dialogView.save.visibility = View.INVISIBLE
dialogView.progressBar.visibility = View.VISIBLE
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459866.html
