Android关闭自定义对话框

标签 android android-dialogfragment android-dialog

我正在尝试让自定义对话框在按钮按下时关闭

        //set up dialog
        Dialog dialog = new Dialog(BrowseActivity.this);
        dialog.setContentView(R.layout.about);
        dialog.setTitle("This is my custom dialog box");
        dialog.setCancelable(true);
        //there are a lot of settings, for dialog, check them all out!

        //set up text
        TextView text = (TextView) dialog.findViewById(R.id.TextView01);
        text.setText(R.string.app_help_message);

        //set up image view
        ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
        img.setImageResource(R.drawable.icon);

      //set up button
        Button button = (Button) dialog.findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
            Dialog.dismiss();

            }
        });

        //now that the dialog is set up, it's time to show it    
        dialog.show();

       return true;

dialog.dismiss 对我不起作用。我只是想将此自定义对话框用作帮助屏幕,并希望按下按钮将其关闭。

我是 android dev 的新手,但已经尝试了很多小时

感谢您的建议

最佳答案

final Dialog dialog = new Dialog(BrowseActivity.this);

你需要小写的对话框。

public void onClick(View v) {
   dialog.dismiss();
}

另外 AlertDialog.Builder 可能是您更好的选择。

关于Android关闭自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6258162/

相关文章:

java - 自定义布局绘制 Canvas 失败

android - Webview 中的 HTML 内容重叠

android - 将具有多个参数的 AsyncTask 转换为 RxJava

android - 旧 Android API 上的 Apple 风格 AlertDialog

android - 是否有可能从它的一个 child 那里获得对 Android Dialog 的引用?

android - ActionBarSherlock onCreateDialog() 错误 : "must override or implement a supertype method"

Android 将数据从 android 发送到服务器作为包

android - 屏幕方向更改后,dialogFragment 显然没有任何调用出现

android - DialogFragment - 获取对话框的宽度

android - 从另一个 fragment :IllegalStateException: Can not perform this action after onSaveInstanceState 启动 dialogfragments