目錄:andorid jar/庫原始碼決議
Butterknife:
作用:
用于初始化界面控制元件,控制元件方法,通過注釋進行系結控制元件和控制元件方法
栗子:
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);
系結界面元素和方法的關聯,
1、傳入當前物件,得到當前物件的類名A,查找A+‘_ViewBinding’組成的類名的,類的建構式,引數是A類物件和View
2、得到類,呼叫他的建構式,函式中通過findViewById,來進行系結(由于A+_ViewBinding是生成的類,該類已知了所有需要系結的控制元件,所以順序處理了,)
3、對于事件方法,則創建了已定義的兼容性的子類,進行呼叫處理,
4、到這里,所有操作就關聯上了,
自動生成了,_ViewBinding類,用于關聯

原始碼:https://github.com/JakeWharton/butterknife
引入:
// 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'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/7431.html
標籤:Android
