简体   繁体   English

组织Android开发代码的最佳方法是什么?

[英]What is the best way to organize android development code?

I know that every situation will be different, but I just wanted see if there was a general recommendation. 我知道每种情况都会有所不同,但我只是想看看是否有一般建议。

Currently, I have my activities (screens) dynamically creating custom button objects and custom edit text objects. 目前,我的活动(屏幕)是动态创建自定义按钮对象和自定义编辑文本对象的。 Each of these objects have listeners to see if their state has changed. 这些对象中的每个对象都有侦听器,以查看其状态是否已更改。 These object classes have all the logic for the screen. 这些对象类具有屏幕的所有逻辑。 The activity's only job is to assign objects to the widgets I created in XML. 该活动的唯一工作是将对象分配给我用XML创建的小部件。

Part of me thinks it should be opposite, where the activity contains all the logic for all the widgets on the screen and simply waits for the objects to notify it when the listeners go off. 我的一部分认为应该相反,该活动包含屏幕上所有小部件的所有逻辑,并且仅在侦听器关闭时等待对象通知它。

Which way is more "standard" ? 哪种方式更“标准”?

I use the following way. 我使用以下方式。 I have a common EventHandler sub class in every activity or fragment and I add a single instance belonging to activity to each UI item. 我在每个活动或片段中都有一个公共的EventHandler子类,并且向每个UI项添加了一个属于活动的实例。 EventHandler implements OnClickListener, OnChanged.., and so on. EventHandler实现OnClickListener,OnChanged ..等。

I would also recommend looking at this library, if you are familiar with DI concept: 如果您熟悉DI概念,我也建议您查看此库:

https://github.com/roboguice/roboguice/ https://github.com/roboguice/roboguice/

Here is an example of code of mine: 这是我的代码示例:

package com.x.y;

public class DashboardActivity extends FragmentActivity {

    private EventHandler eventHandler = new EventHandler();

    @SomeAnnotationForInit(R.id.some_id)
    private Button feedButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_activity);

        initGui();
    }

    private void initGui() {
        feedButton.setOnClickListener(eventHandler);
    }

    private class EventHandler implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if(view.equals(...)) {
                //TODO:
            }
        }
    }
}

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

相关问题 组织SDK + NDK模块的最佳方法是什么 - What is the best way to organize an SDK + NDK module 将Android项目组织到Subversion存储库中的最佳方法 - Best way to organize a android project into a subversion repository Android开发:保留开发文件的最佳方法 - Android development: Best way to keep development files Android 中的 MVC 是/否? 组织与网络服务通信的应用程序的最佳方式是什么 - MVC in Android Yes/No? What is the best way to organize an app which communicates with a webservices 在Eclipse中调试android代码的最佳方法是什么? - What is the best way to debug the android code in Eclipse? 为Android应用组织可编辑内部数据文件的最佳方法? - Best way to organize editable internal data files for Android app? Android:组织显示警报对话框的方法的最佳实践是什么 - Android: What is the best practice to organize methods to show alert dialog 实践中的Android ADK开发-什么是最佳实践? - Android ADK development in practice - what are best practices? Delphi-XE5 Android开发。 释放非模式形式的最佳方法是什么? - Delphi-XE5 Android Development. What is the best way to free non-modal forms? 从Android中的代码向布局添加视图的最佳方法是什么? - What is the best way to add views to layout from code in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM