目錄:andorid jar/庫源碼解析 html
用於初始化界面控件,控件方法,經過註釋進行綁定控件和控件方法android
public class MainActivity extends AppCompatActivity { @BindView(R.id.btnTest1) Button btnTest1; @BindView(R.id.btnTest2) Button btnTest2; @BindView(R.id.lblMsg) TextView lblMsg; @BindView(R.id.txtMsg) EditText txtMsg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.btnTest1) void test1(){ Toast.makeText(this, txtMsg.getText().toString(), Toast.LENGTH_LONG).show(); } @OnClick(R.id.btnTest2) void test2(){ String msg = "test2222222222"; lblMsg.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } }
ButterKnife.bind(this);
綁定界面元素和方法的關聯。ide
一、傳入當前對象,獲得當前對象的類名A,查找A+‘_ViewBinding’組成的類名的,類的構造函數,參數是A類對象和View函數
二、獲得類,調用他的構造函數,函數中經過findViewById,來進行綁定(因爲A+_ViewBinding是生成的類,該類已知了全部須要綁定的控件,因此順序處理了。)this
三、對於事件方法,則建立了已定義的兼容性的子類,進行調用處理。spa
四、到這裏。全部操做就關聯上了。code
自動生成了,_ViewBinding類,用於關聯htm
// androidx implementation 'com.jakewharton:butterknife:10.0.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0' // android.support.v4.content // implementation 'com.jakewharton:butterknife:8.8.1' // annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'