Android - Custom Dialog - Can't get text from EditText

前端 未结 6 770
清歌不尽
清歌不尽 2020-12-01 12:00

I have a problem with a custom dialog.
My dialog consists of a TextView, EditText and an \"Ok\" Button. After clicking \"Ok\", it should get t

6条回答
  •  無奈伤痛
    2020-12-01 12:52

    You are inflating a layout where it is not needed. I fixed your code as you see I removed your line where it inflates and changed the line where you try to find the EditText view.

    final Dialog dialog = new Dialog(MyActivity.this);
     dialog.setContentView(R.layout.custom_dialog);
     dialog.setTitle("Title");
    
     Button button = (Button) dialog.findViewById(R.id.dialog_ok);
     button.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
    
                EditText edit=(EditText)dialog.findViewById(R.id.dialog_edit);
                String text=edit.getText().toString();
    
                dialog.dismiss();
                name=text;
    
         }
     });   
    
    
    dialog.show();
    

提交回复
热议问题