欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android注解ButterKnife的基本使用

 更新時(shí)間:2017年01月11日 14:21:55   作者:慌不擇食  
這篇文章主要介紹了Android注解ButterKnife的基本使用的相關(guān)資料,需要的朋友可以參考下

ButterKnife的最新版本是8.4.0。

首先,需要導(dǎo)入ButterKnife的jar包。

在AndroidStudio中,File->Project Structure->Dependencies->Library dependency 搜索butterknife即可,第一個(gè)就是.

另外一種就是直接在build:grade(app)dependencies里添加:

compile 'com.jakewharton:butterknife:8.4.0' 
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 

ok,現(xiàn)在正式開(kāi)始使用吧,用法也很簡(jiǎn)單

在Activity子類的onCreate()方法里使用ButterKnife.bind(this);即可

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    tv1.setText("hi!sy") 

注意:一定要在setContentView之后寫(xiě)。

再然后,把光標(biāo)放在R.layout.activity_main上,鼠標(biāo)右擊,選中Generate...(Alt+Insert),點(diǎn)擊會(huì)出現(xiàn):

然后這樣

選中的有TextView點(diǎn)擊事件和findViewById的注解,點(diǎn)擊Confirm就成功了!

什么,你說(shuō)沒(méi)有,別著急,你需要安裝一個(gè)小插件(不要嫌麻煩,其實(shí)很簡(jiǎn)單,一勞永逸)

AndroidStudio->File->Settings->Plugins->搜索Zelezny下載添加就行 ,可以快速生成對(duì)應(yīng)組件的實(shí)例對(duì)象,不用手動(dòng)寫(xiě)。

使用時(shí),在要導(dǎo)入注解的Activity 或 Fragment 或 ViewHolder的layout資源代碼上,右鍵——>Generate——Generate ButterKnife Injections。

源碼

public class MainActivity extends Activity { 
   @BindView(R.id.tv_time) 
  TextView tvTime; 
  @BindView(R.id.activity_main) 
  RelativeLayout activityMain; 
  @BindView(R.id.tv_cal) 
  TextView tvCal; 
  @BindView(R.id.tv_date) 
  TextView tvDate; 
  Time time; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    tvTime.setText("Time類"); 
    tvCal.setText("Calender類"); 
    tvDate.setText("Date類"); 
    initTime(); 
  } 
  private void initTime() { 
    time = new Time(); 
    time.setToNow(); 
  } 
  @OnClick({R.id.tv_cal, R.id.tv_date,R.id.tv_time}) 
  public void onClick(View view) { 
    switch (view.getId()) { 
      case R.id.tv_time://點(diǎn)擊第一個(gè) 
        String times = time.year + "年" + time.month + "月" + time.monthDay 
            + "日" + time.hour + "時(shí)" + time.minute + "分" + time.second + "秒" 
            + ":現(xiàn)在是一年中的第" + time.yearDay + "天"; 
        Toast.makeText(this, Time.getCurrentTimezone() + times, Toast.LENGTH_SHORT).show(); 
        tvTime.setText(times); 
        break; 
      case R.id.tv_cal: 
        break; 
      case R.id.tv_date: 
        break; 
    } 
  } 
  @Override 
  protected void onDestroy() { 
    super.onDestroy(); 
//    Unbinder unbinder=ButterKnife.bind(this); 
//    unbinder.unbind(); 
    ButterKnife.bind(this).unbind(); 
  } 
} 

以上所述是小編給大家介紹的Android注解ButterKnife的基本使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論