Data Binding remove annoying findViewById

Android notes

1. You may have heard of Jake Wharton's ButterKnife. This library only needs to pass in the corresponding id through annotations when defining the View variable, and then call ButterKnife.bind (this) during onCreate to complete the injection of the view. Examples are as follows:

class ExampleActivity extends Activity {
  @BindView(R.id.user) EditText username;
  @BindView(R.id.pass) EditText password;

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

Source: http://www.jianshu.com/p/eb29c691d370

Official translation: http://yanghui.name/blog/2016/02/17/data-binding-guide/

Introduction: http://blog.csdn.net/qq_33689414/article/details/52205703

二 、
Android Data Binding

Ready to work

First, you need to meet a condition: your Android Plugin for Gradle version must be equal to or higher than 1.5.0-alpha1 version, this version is located in the root directory build.gradle, examples are as follows:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0-rc1'
    }
}

Next, you must tell the compiler to enable Data Binding, which is generally located in the android tag of app: build.gradle. Examples are as follows:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dataBinding {
        enabled true
    }
    ...
}
Published 34 original articles · Like 10 · Visits 30,000+

Guess you like

Origin blog.csdn.net/q296264785/article/details/68926420