Android close custom dialog

徘徊边缘 提交于 2019-12-08 14:34:04

问题


I am trying to get the custom dialog to close on button press

        //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 is not working for me. I am simply trying to use this custom dialog as a help screen and want a button press to close it.

I'm very new to android dev but have been trying this for many many hours

Thanks for any advise


回答1:


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

You need lowercase dialog.

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

Also AlertDialog.Builder may be a better choice for you.




回答2:


You may call dismiss(); on the dialog. This work for me.



来源:https://stackoverflow.com/questions/6258162/android-close-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!