How to make a edittext box in a dialog

后端 未结 8 1867
借酒劲吻你
借酒劲吻你 2020-11-28 01:34

I am trying to make a edittext box in a dialog box for entering a password. and when I am doing I am not able to do. I am a beginner in it. Please help me in this.



        
8条回答
  •  春和景丽
    2020-11-28 02:07

    Setting margin in layout params will not work in Alertdialog. you have to set padding in parent layout and then add edittext in that layout.

    This is my working kotlin code...

    val alert =  AlertDialog.Builder(context!!)
    
    val edittext = EditText(context!!)
    edittext.hint = "Enter Name"
    edittext.maxLines = 1
    
    val layout = FrameLayout(context!!)
    
    //set padding in parent layout
    layout.setPaddingRelative(45,15,45,0)
    
    alert.setTitle(title)
    
    layout.addView(edittext)
    
    alert.setView(layout)
    
    alert.setPositiveButton(getString(R.string.label_save), DialogInterface.OnClickListener {
    
        dialog, which ->
        run {
    
            val qName = edittext.text.toString()
    
            Utility.hideKeyboard(context!!, dialogView!!)
    
        }
    
    })
    alert.setNegativeButton(getString(R.string.label_cancel), DialogInterface.OnClickListener {
    
                dialog, which ->
                run {
                    dismiss()
                }
    
    })
    
    alert.show()
    

提交回复
热议问题