无法访问 AlertDialog 内的视图

Gee*_*bey 2 android android-alertdialog

我从这里复制了整个代码

Android自定义数字键盘

并在AlertDialog. 现在,当我调试应用程序时,onClick()不会调用该应用程序。

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setView(R.layout.keypad_layout);
                builder.setCancelable(false).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.setPositiveButton("Modify", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.create().show();\
Run Code Online (Sandbox Code Playgroud)

警报对话框正在显示,正负按钮可以工作,唯一的问题是我无法访问布局内的视图

Gee*_*bey 6

事实证明,AlertDialog返回一个view

AlertDialog dialog = builder.create();
dialog.show();
mPasswordField = (EditText) dialog.findViewById(R.id.password_field);
Run Code Online (Sandbox Code Playgroud)

这对我来说很有效。