简体   繁体   English

如何从MainActivity类文件扩展活动?

[英]How to extend an activity from the MainActivity class file?

The UI elements in the MainActivity class need to be preserved and the other class file adds a new UI element to the main xml layout that's used by both classes to differing degrees. 需要保留MainActivity类中的UI元素,另一个类文件将新的UI元素添加到主要xml布局中,这两个类在不同程度上使用了该元素。

It's understood that you can create a base abstract class and two concrete inherited classes however in this case there a third supporting class for the second (other class) and it requires a handler to function. 可以理解,您可以创建一个基本抽象类和两个具体的继承类,但是在这种情况下,第二个(其他类)需要第三个支持类,并且需要一个处理程序来起作用。

For perspective, a button (in activity_main ) is clicked and it should launch an activity while maintaining the UI elements used MainActivity . 为了进行透视,单击了一个按钮(位于activity_main ),它应该启动一个活动,同时保持使用MainActivityUI元素。 Furthermore the button has it's own class file methods and isn't in MainActivity . 此外,该按钮具有其自己的类文件方法,并且不在MainActivity

What happens now? 现在会发生什么? The button is pressed and nothing happens. 按下该按钮,没有任何反应。 Manifest confirmed so its not that. 清单证实不是这样。 Or I allow the main activity or the other activity and it works, both need to work simultaneously. 或者我允许主要活动或其他活动同时起作用,两者都需要同时工作。

Basically MainActivity needs to act as the base abstract activity for the separate class file. 基本上, MainActivity需要充当单独的类文件的基本抽象活动。

You are messing up activities and views. 您正在弄乱活动和视图。 To reuse the same business logic, you can write common logic in base class for all other activities (ie class BaseActivity extends Activity ). 要重用相同的业务逻辑,您可以在所有其他活动的基类中编写通用逻辑(即class BaseActivity extends Activity )。 To reuse different UI parts you should either use fragments or you can use <include>/<merge> tags pair to include a certain layout into another layout. 要重用不同的UI部分,您应该使用片段,也可以使用<include>/<merge>标签对将某个布局包含到另一个布局中。

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

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