简体   繁体   English

启动器活动覆盖活动布局中的自定义对话框

[英]Custom dialog in launcher activity overwriting activity layout

I have a dialog in my launcher activity that requires some user input and to display it I have this code in onCreate()... 我的启动器活动中有一个对话框,需要一些用户输入,要显示该对话框,我在onCreate()中有此代码...

 final Dialog dialog = new Dialog(this);
 dialog.setContentView(R.layout.dialog);
 DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);
 popupBinding.dialogButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           dialog.dismiss();
       }
   });
 dialog.show();

The dialog button has an onClick listener to close it, but in order to do that, the dialog must be declared final. 对话框按钮具有一个onClick侦听器以将其关闭,但是为此,必须将对话框声明为final。 Previously (when this activity was not the launcher), I had the dialog declared as an instance variable, but this causes an error now. 以前(当此活动不是启动程序时),我已将对话框声明为实例变量,但这现在导致错误。

private Dialog dialog = new Dialog(this); 

However, declaring the dialog in the onCreate method causes the launcher activity's layout to be replaced with the dialog's so that the dialog appears over a copy of itself. 但是,在onCreate方法中声明对话框会使启动程序活动的布局替换为对话框的布局,从而使对话框出现在其自身的副本上方。

带对话框的启动器活动

I'm not sure why it is doing that, but I was wondering if there is a way to prevent this. 我不确定为什么要这样做,但是我想知道是否有办法防止这种情况发生。 Thanks! 谢谢!

This accidentily sets the dialog layout as the content view for your activity. 这会意外地将对话框布局设置为您的活动的内容视图。

DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);

So you're mixing your activity with the dialog. 因此,您正在将活动与对话框混在一起。

For your Activity, this should be something like 对于您的活动,这应该类似于

MyActivityBinding activityBinding = DataBindingUtil.setContentView(this, R.layout.my_activity);

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

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