How to make a edittext box in a dialog

后端 未结 8 1830
借酒劲吻你
借酒劲吻你 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 01:59

    I know its too late to answer this question but for others who are searching for some thing similar to this here is a simple code of an alertbox with an edittext

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    

    or

    new AlertDialog.Builder(mContext, R.style.MyCustomDialogTheme);
    

    if you want to change the theme of the dialog.

    final EditText edittext = new EditText(ActivityContext);
    alert.setMessage("Enter Your Message");
    alert.setTitle("Enter Your Title");
    
    alert.setView(edittext);
    
    alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //What ever you want to do with the value
            Editable YouEditTextValue = edittext.getText();
            //OR
            String YouEditTextValue = edittext.getText().toString();
        }
    });
    
    alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // what ever you want to do with No option.
        }
    });
    
    alert.show();
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题