How to create a number picker dialog?

前端 未结 5 2211
误落风尘
误落风尘 2020-11-28 05:40

I want to be able to create a Dialog that allows the user to pick a number from a specified range.

I know that there are existing widgets(like those from quietlycod

5条回答
  •  無奈伤痛
    2020-11-28 06:01

    For kotlin lovers.

        fun numberPickerCustom() {
            val d = AlertDialog.Builder(context)
            val inflater = this.layoutInflater
            val dialogView = inflater.inflate(R.layout.number_picker_dialog, null)
            d.setTitle("Title")
            d.setMessage("Message")
            d.setView(dialogView)
            val numberPicker = dialogView.findViewById(R.id.dialog_number_picker)
            numberPicker.maxValue = 15
            numberPicker.minValue = 1
            numberPicker.wrapSelectorWheel = false
            numberPicker.setOnValueChangedListener { numberPicker, i, i1 -> println("onValueChange: ") }
            d.setPositiveButton("Done") { dialogInterface, i ->
                println("onClick: " + numberPicker.value)
    
            }
            d.setNegativeButton("Cancel") { dialogInterface, i -> }
            val alertDialog = d.create()
            alertDialog.show()
        }
    

    and number_picker_dialog.xml

    
    
    
    

提交回复
热议问题