将一个相当大的 android 项目分解为较小的组件
我对开发 Android 程序还是很陌生,并且在当前的项目中遇到了麻烦。它一开始只是一个简单的小项目,但是对于一个文件来说它已经变得很大了。
我想将其分成几个组件,例如 ui、计算和 main。但最好的方法是什么?
目前我只有 :
public class main extends Activity {...}
,
public class preference extends PreferenceActivity implements OnSharedPreferenceChangeListener {...}
我是否应该从 main 中删除这些函数并将其放入例如:
public class ui extends main {...}
I'm still quite new to developing android programs and getting in to trouble with my current project. It started of as a simple small project but it is getting way to big for one file.
I would like to split it up in several components for example ui, calculations and main. But what is the best way to do this.
Currently I've only got :
public class main extends Activity {...}
and
public class preference extends PreferenceActivity implements OnSharedPreferenceChangeListener {...}
Should I remove the functions from main an put it in for example:
public class ui extends main {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以相应地组织您的项目。我的做法是拥有以下包:
activities:我放置所有活动的位置。
类型:我放置所有类型的位置,例如:
用户
或国家/地区
。例外:我放置所有自定义例外的位置。
服务。
providers:针对内容提供商。
首选项。
util:一个包,其中包含可能用作工具的所有类(例如 JSON 解析器、字符串实用程序类、正则表达式等)。
等等。
在
activities
包中,您可以按活动添加一个类。调用一个类ui
并不是一个好主意,ui
可能是包的名称(而不是活动的名称)。您应该以描述活动功能的方式调用您的类。例如,您可以将其称为:LoginScreenActivity
。我鼓励您查看一些开源项目,例如 Shelves 或 Foursquare 研究组织 Android 项目的可能方法。It is up to you to organize your project accordingly. My way of doing things is to have the following packages:
activities: Where I place all the activties.
types: Where I place all the types, like:
User
orCountry
.exceptions: Where I place all my custom exceptions.
services.
providers: for the content providers.
preferences.
util: a package that holds all the classes that might be useful as a tool (like JSON parsers, String utility classes, Regular expressions etc.).
etc.
Inside the
activities
package you could add one class by activity. Calling one classui
is not a good idea,ui
could be the name of the package (insead of activities). You should call your class in a way that it describes the functionality of your activity. For instance you can call it:LoginScreenActivity
. I encourage you to check some open source projects like Shelves or Foursquare to study a possible way of organizing your android projects.我会遵循 MVC/MVP/MVVM 方法。我目前也在一个大型 Android 项目中,我正在尝试按模型/视图/命名空间划分我的文件。
I would follow a MVC/MVP/MVVM approach. I'm currently in a big Android project too, and I'm trying to divide my files by a model/view/ namespaces.