使onCreate方法看起来更有条理

时间:2015-08-28 11:08:41

标签: android

我的Widgets中有很多MainActivity,他们都必须被初始化。但是当我在OnCreate方法中初始化它们时,OnCreate方法看起来不再有条理。那么我应该在我的MainActivity中使用另一种方法初始化它们吗?

3 个答案:

答案 0 :(得分:1)

是的,您可以使用像Butter Knife这样的框架来减少代码。 http://jakewharton.github.io/butterknife/

答案 1 :(得分:1)

...绝对 你的代码必须是可读的,不是每个人都想到它。!! :( 举个例子如下......

ublic class ActHome extends AppCompatActivity implements View.OnClickListener {

    RelativeLayout layoutToBeHidden;
    TextView tvName;
    Button btnOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      // do the preprocessing here
      setContentView(R.layout.activity_home);
      initialize();
      populate();
    }


    private void initialize() {
      // bind all your view from xml here
        layoutToBeHidden = (RelativeLayout) findViewById(R.id.LayoutToBeHiddenActHome);
              tvName = (RelativeLayout) findViewById(R.id.tvNameActHome);
              btnOk = (RelativeLayout) findViewById(R.id.btnOkActHome);
      
      //then set all the listeners etc.
      btnOk.setOnClickListener(this);

    }
  
      private void populate() {
      // populate the data here e.g. from database etc.
        // and bind this data to the view etc.
        String name = "android";
        tvName.setText(name);

    }

    
    @Override
    public void onClick(View v) {
     Toast.makeText(context, "OK clicked", Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:0)

您可以使用RoboGuice库来提高onCreate()的可读性。

RoboGuice 3减少了您的应用程序代码。代码越少意味着错误的机会就越少。它还使您的代码更容易理解 - 不再是您的代码充斥着Android平台的机制,但现在它可以专注于您的应用程序独有的实际逻辑。

为了给您一个想法,请看一下典型Android活动的这个简单示例:

class AndroidWay extends Activity { 
        TextView name; 
        ImageView thumbnail; 
        LocationManager loc; 
        Drawable icon; 
        String myName; 

        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main);
            name      = (TextView) findViewById(R.id.name); 
            thumbnail = (ImageView) findViewById(R.id.thumbnail); 
            loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); 
            icon      = getResources().getDrawable(R.drawable.icon); 
            myName    = getString(R.string.app_name); 
            name.setText( "Hello, " + myName ); 
        } 
    } 

这个例子是19行代码。如果您正在尝试通读onCreate(),则必须跳过5行样板初始化才能找到唯一真正重要的内容:name.setText()。复杂的活动最终会产生更多这种初始化代码。

将此与使用RoboGuice编写的相同应用进行比较:

@ContentView(R.layout.main)
    class RoboWay extends RoboActivity { 
        @InjectView(R.id.name)             TextView name; 
        @InjectView(R.id.thumbnail)        ImageView thumbnail; 
        @InjectResource(R.drawable.icon)   Drawable icon; 
        @InjectResource(R.string.app_name) String myName; 
        @Inject                            LocationManager loc; 

        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            name.setText( "Hello, " + myName ); 
        } 
    } 

在此示例中,onCreate()更容易一目了然。

RoboGuice's的目标是让您的代码与您的应用相关,而不是通常需要在Android中维护的所有初始化和生命周期代码。

我希望它有所帮助!