Android – Best Practices for loading UI components in activities

Is there a convention or best practice for binding UI elements to variables in an activity? I usually do something similar:

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

private void loadElements(){

button1=(Butotn)findViewByID(R.id.button1);
txV1=(TextView)findViewByID(R.id.texview1);

 }

I read findviewbyid() somewhere. It may be very tired. Is this true?

resolvent:

Although there is no real agreement, you can consider using a library called butterknife (there are some other libraries, but this library seems to be very popular), which can do this for you without writing all the boring template code. Therefore, your code can be reduced to the following:

@Bind(R.id.button1)
Button button1;

@Bind(R.id.texview1)
TextView txV1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>