简体   繁体   English

在运行时为对话框选择布局

[英]Choosing layout for a dialog at runtime

I need to extend DialogFragment to create dialogs that take user input and store them to be manipulated by another class. 我需要扩展DialogFragment来创建接受用户输入并存储它们以供其他类操纵的对话框。 There are around six different layouts that could need to be displayed depending on which option the user selects. 根据用户选择的选项,可能需要显示大约六种不同的布局。 Extending dialogfragment six times doesnt sit well with me as a devoted initiate into OOP. 作为致力于OOP的同修,六次扩展dialogfragment并不适合我。 My question then is how could I pass a layout to the DialogFragment at runtime. 然后我的问题是如何在运行时将布局传递给DialogFragment。

I have tried both creating a class variable and a getter method in AdvantageDialog both to no avail. 我尝试在AdvantageDialog中创建类变量和getter方法都无济于事。

It's instantiated like this 像这样实例化

    DialogFragment newFragment = new AdvantageDialog();
    newFragment.show(getFragmentManager(), "add_advantage");

but then newFragment.inputScreen nor newFragment.setInputScreen(some int) can be accessed from outside (ie. "setInputScreen(int) is undefined for the type dialogfragment"). 但随后可以从外部访问newFragment.inputScreen或newFragment.setInputScreen(some int)(即,“ dialogfragment类型的setInputScreen(int)未定义”)。
I guess it would work if I did AdvantageDialog aD = new AdvantageDialog and then aD.setInputScreen(some int) but then I wouldnt benefit from the DialogFragment handling lifecycle methods. 我想如果我先执行AdvantageDialog aD = new AdvantageDialog,然后再执行aD.setInputScreen(some int),那将行得通,但是我将无法从DialogFragment处理生命周期方法中受益。 Also as far as I can see also there are no methods in the API for setting the view, only possible to change the view container. 据我所知,API中也没有设置视图的方法,只能更改视图容器。

Please any idea how to do this or can you direct me to where exactly my understanding is lacking. 请任何想法如何执行此操作,或者您可以将我定向到我完全缺乏理解的地方。 It would be greatly appreciated. 这将不胜感激。 Andy 安迪

Fragment has the setArgument and getArgument method. 片段具有setArgumentgetArgument方法。 You can provide the layout id through the bundle that seArgument takes as parameter and getArgument returns, and use this id inside onCreateView to inflate the corresponding view's object 您可以通过seArgument作为参数和getArgument返回的包提供布局ID,并在onCreateView使用该ID来膨胀相应视图的对象

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

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