How to display year only in date picker in android

前端 未结 6 1304
一整个雨季
一整个雨季 2020-12-03 21:59

Can anybody tell how to display year only in date picker in android

Thanks

6条回答
  •  Happy的楠姐
    2020-12-03 22:26

    I have modified @agilanbu 's answer and it can be used as :

    private fun createDialogWithoutDateField() {
    
        val alertDialog: AlertDialog?
        val builder = AlertDialog.Builder(activity, R.style.AlertDialogStyle)
        val inflater = activity!!.layoutInflater
    
        val cal = Calendar.getInstance()
    
        val dialog = inflater.inflate(R.layout.month_year_picker_dialog, null)
        val monthPicker = dialog.findViewById(R.id.picker_month) as NumberPicker
        val yearPicker = dialog.findViewById(R.id.picker_year) as NumberPicker
    
        monthPicker.minValue = 1
        monthPicker.maxValue = 12
        monthPicker.value = cal.get(Calendar.MONTH) + 1
    
        val year = cal.get(Calendar.YEAR)
        yearPicker.minValue = 1900
        yearPicker.maxValue = 3500
        yearPicker.value = year
    
        builder.setView(dialog).setPositiveButton(Html.fromHtml("Ok")){dialogInterface, which ->
            //Toast.makeText(applicationContext,"clicked yes",Toast.LENGTH_LONG).show()
    
            val value = yearPicker.value
            dialogInterface.cancel()
        }
    
        builder.setNegativeButton(Html.fromHtml("Cancel")){dialogInterface, which ->
            dialogInterface.cancel()
        }
    
        alertDialog = builder.create()
        // Set other dialog properties
        alertDialog.setCancelable(true)
        alertDialog.show()
    }
    

    You can get the layout file and styles in @agilanbu 's answer.

提交回复
热议问题