且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android - 自定义对话框 - 无法从 EditText 获取文本

更新时间:2023-01-23 21:07:11

您正在对不需要的布局进行膨胀.如您所见,我修复了您的代码,我删除了您的膨胀行并更改了您尝试查找 EditText 视图的行.

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();