Kotlin - 如何从对话框中的按钮选择中获取价值?
Kotlin - How to get value from button choice in Dialog?
我正在创建一个高尔夫记分卡应用程序。
每个洞的分数都是一个空的 textView,而该 textView 有一个 setOnClickListener 来打开分数选择器对话框。
我想从分数选择器对话框中获取分数值。
对话界面如下:
https://i.stack.imgur.com/VNVc1.png
每个按钮对应一个分数。
我知道每个按钮都需要一个 setOnClickListener,但我对之后的其他一切了解有限。
所以我的问题是如何 return 该得分值,以便我可以在特定的 textView 中显示它并将其添加到玩家的总分中?任何建议都会很有帮助。
感谢您的宝贵时间。
您可以实现自定义监听器,您需要的监听器
以下代码用于演示,
在 dialogfragment
中实现
//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")
可能这段代码对您来说很简单。使用 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()
}
So my question is how to return that score value so I can display it in that specific the textView and add it to the player's total? Any suggestions would be very helpful.
https://developer.android.com/guide/topics/ui/dialogs#PassingEvents
我正在创建一个高尔夫记分卡应用程序。 每个洞的分数都是一个空的 textView,而该 textView 有一个 setOnClickListener 来打开分数选择器对话框。 我想从分数选择器对话框中获取分数值。
对话界面如下: https://i.stack.imgur.com/VNVc1.png
每个按钮对应一个分数。
我知道每个按钮都需要一个 setOnClickListener,但我对之后的其他一切了解有限。
所以我的问题是如何 return 该得分值,以便我可以在特定的 textView 中显示它并将其添加到玩家的总分中?任何建议都会很有帮助。
感谢您的宝贵时间。
您可以实现自定义监听器,您需要的监听器 以下代码用于演示, 在 dialogfragment
中实现//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")
可能这段代码对您来说很简单。使用 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()
}
So my question is how to return that score value so I can display it in that specific the textView and add it to the player's total? Any suggestions would be very helpful.
https://developer.android.com/guide/topics/ui/dialogs#PassingEvents