Android butterknife使用

第一步:在工程下build.gradle文件,在 dependencies下引用

classpath "com.jakewharton:butterknife-gradle-plugin:10.1.0"

第二步:在APP工程build.gradle文件,在dependencies下引用

implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

第三步:添加成功後,把光標定位在activity_main的後面,注意是括號裏邊
setContentView(R.layout.activity_main);
右擊選擇Generate... 選擇最後一行  或者使用快捷鍵Alt + Insert選擇即可快速添加所有佈局ID

第四步:

activity使用

@BindView(R.id.xxx)
EditText editText;

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

fragment使用

Unbinder unbinder;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); unbinder = ButterKnife.bind(this, view);
}
@Override public void onDestroy() { super.onDestroy(); unbinder.unbind(); }

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