【问题标题】:Kotlin - How to get value from button choice in Dialog?Kotlin - 如何从对话框中的按钮选择中获取价值?
【发布时间】:2021-06-09 01:32:06
【问题描述】:

我正在创建一个高尔夫记分卡应用程序。 每个洞的得分都是一个空的 textView,并且该 textView 有一个 setOnClickListener 来打开得分选择器对话框。 我想从分数选择器对话框中获取分数值。

对话框界面如下: https://i.stack.imgur.com/VNVc1.png

每个按钮对应一个分数。

我知道每个按钮都需要一个 setOnClickListener,但我对之后的其他所有内容了解有限。

所以我的问题是如何返回该分数值,以便我可以在特定的 textView 中显示它并将其添加到玩家的总数中?任何建议都会非常有帮助。

感谢您的宝贵时间。

【问题讨论】:

    标签: android kotlin dialog


    【解决方案1】:

    您可以实现自定义侦听器,即您需要的侦听器 以下代码用于演示, 在对话框片段中实现

    //score for current hole dialog
    class SomeDialog:DialogFragment() {
        private var dl: CustomListener? = null
    
        // interface to detect dialog close event
        interface CustomListener {
            fun closeEvent(id: String, valueToPass: Int)
        }
    
        fun customListerTrig(l: CustomListener) {
            dl = l
        }
    
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
    
            button1.setOnClickListener {
                dl?.closeEvent("golfDiag", 1)
                this.dismiss()
            }
            button2.setOnClickListener {
                dl?.closeEvent("golfDiag", 2)
                this.dismiss()
    
            }
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_some_dialog, container, false)
        }
    
    }
    
    

    我如何调用和检索按钮点击事件

     val common = SomeDialog()
         common.customListerTrig(object : CommonInfoDialog.CustomListener {
                override fun closeEvent(id: String, buttonValueFromDialog: Int) {
                    // todo usebuttonValueFromDialog
                }
          })
          //use fragment according where this dialog will be called.
          common.show(this.childFragmentManager, "golfDiag")
    

    【讨论】:

    • @AnhLa 谢谢,现在我可以在任何地方发表评论,因为这个答案我得到了 +15,所以现在我可以帮助更多的人 :)
    【解决方案2】:

    也许这段代码对你来说很简单。使用 Android 对话框 API 让您的工作更轻松。

       /**
         * e.g.
        showPickScoreDialog(requireContext()) { score ->
            Toast.makeText(context, "score[$score]", Toast.LENGTH_LONG).show()
        }
         */
        fun showPickScoreDialog(context: Context, callback: (Int) -> Unit) {
            val defaultCheckedScore = -1
            val scoreList = getScoreList()
            MaterialAlertDialogBuilder(context)
                .setTitle("Score For Current Hole:")
                .setSingleChoiceItems(scoreList, defaultCheckedScore) { dialog: DialogInterface?, which: Int ->
                    val score = scoreList[which].toInt()
                    callback(score)
                    dialog?.dismiss()
                }
                .setNegativeButton("Cancel", null)
                .show()
        }
    
        private fun getScoreList(): Array<String> {
            val list = mutableListOf<String>()
            for (i in 1..12) {
                list.add(i.toString())
            }
            return list.toTypedArray()
        }
    

    【讨论】:

      【解决方案3】:

      所以我的问题是如何返回该分数值,以便我可以在特定的 textView 中显示它并将其添加到玩家的总数中?任何建议都会非常有帮助。

      https://developer.android.com/guide/topics/ui/dialogs#PassingEvents

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-20
        • 2013-10-25
        • 2018-02-03
        • 2022-01-22
        • 2015-03-06
        相关资源
        最近更新 更多