简体   繁体   English

.dismiss()在AlertDialog中显示错误

[英].dismiss() is showing an error in AlertDialog

I have the following code: 我有以下代码:

            AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.displayfilecontents, null);
            EditText text = (EditText) view.findViewById(R.id.etFileContents);
            if (text != null) {
                text.setFocusable(false);
                text.setLongClickable(false);
                text.setTextIsSelectable(false);
            }
            text.setText(builder);
            b.setView(view);
            b.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
            Button btnCloseIt = (Button) view.findViewById(R.id.btnClose);
            btnCloseIt.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    b.dismiss();
                }
            });
            AlertDialog dl = b.create();
            dl.show();

I am trying to dismiss the dialog once the btnCloseIt is pressed. 我想一旦关闭该对话框btnCloseIt被按下。 I am receiving an error on this line: 我在这条线上收到错误:

b.dismiss(); //giving an error

Error: The method dismiss() is undefined for the type AlertDialog.Builder 错误: The method dismiss() is undefined for the type AlertDialog.Builder

Update : [ RESOLVED ] 更新 :[ 已解决 ]

        // custom dialog
        final Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(R.layout.displayfilecontents);
        dialog.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));

        EditText text = (EditText) dialog.findViewById(R.id.etFileContents);
        if (text != null) {
            text.setFocusable(false);
            text.setLongClickable(false);
            text.setTextIsSelectable(false);
        }
        text.setText(builder);
        Button btnCloseIt = (Button) dialog.findViewById(R.id.btnClose);
        // if button is clicked, close the custom dialog
        btnCloseIt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();

As others have already pointed out, b is a reference to AlertDialog.Builder and not to the Dialog itself. 正如其他人已经指出的那样, b是对AlertDialog.Builder的引用,而不是对Dialog本身的引用。 AlertDialog.Builder class doesn't have any method named dismiss() . AlertDialog.Builder类没有名为dismiss()任何方法。 Save a reference to the Dialog which is returned to you when you call create() or show() method from AlertDialog.Builder class. 保存对Dialog的引用,当您从AlertDialog.Builder类调用create()show()方法时,该Dialog将返回给您。

One more thing, since you are calling create() and show() methods at the same time, do you really want to call both the methods? 还有一件事,因为您要同时调用create()show()方法,您真的要同时调用这两个方法吗? I believe calling only show() method would suffice for you here. 我相信只调用show()方法就足够了。 From Developer Reference public AlertDialog show () : Creates a AlertDialog with the arguments supplied to this builder and show()'s the dialog. 来自开发人员参考的公共AlertDialog show():创建一个AlertDialog,其中提供给此构建器的参数以及show()的对话框。

You need to store the result of calling b.create() ; 您需要存储调用b.create()的结果; that's what you need to call dismiss() on. 这就是您需要调用dismiss()的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM