ButterKnife8.0.2的使用例程

最近在使用JakeWharton大神的BufferKnife(github地址:https://github.com/JakeWharton/butterknife),發現這個庫太好用了,徹底擺脫了findViewById()這個這麼長的方法。但是在版本8.0.0之前都是直接Compile “xxxxxxxxxxx”就可是使用了,但是8.0.0之後的依賴引用有點區別了。

PS:發現自己的工程取名字的時候把Butter寫成了Buffer了!湊合看

首先在工程級build.gradle文件中添加:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
下面是添加後的文件內容:


然後在Module級的build.gradle中添加:

apply plugin: 'android-apt'    //這個必須在文件頂部添加

compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'

添加後文件部分內容


現在就可以開始使用了

在佈局文件中添加一個textview

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
在activity中

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.text)
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        textView.setText("1111111111");
    }

}
程序結果


源代碼


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章