最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android注解ButterKnife的基本使用

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

ButterKnife的最新版本是8.4.0。

首先,需要導入ButterKnife的jar包。

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

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

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

ok,現在正式開始使用吧,用法也很簡單

在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之后寫。

再然后,把光標放在R.layout.activity_main上,鼠標右擊,選中Generate...(Alt+Insert),點擊會出現:

然后這樣

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

什么,你說沒有,別著急,你需要安裝一個小插件(不要嫌麻煩,其實很簡單,一勞永逸)

AndroidStudio->File->Settings->Plugins->搜索Zelezny下載添加就行 ,可以快速生成對應組件的實例對象,不用手動寫。

使用時,在要導入注解的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://點擊第一個 
        String times = time.year + "年" + time.month + "月" + time.monthDay 
            + "日" + time.hour + "時" + time.minute + "分" + time.second + "秒" 
            + ":現在是一年中的第" + 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的基本使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

最新評論

西畴县| 尼木县| 拜泉县| 长白| 荣昌县| 天门市| 大洼县| 文化| 宜章县| 邮箱| 常山县| 图木舒克市| 靖州| 宣汉县| 桓仁| 即墨市| 灵寿县| 卢湾区| 长岛县| 炎陵县| 石楼县| 建昌县| 扎鲁特旗| 阿坝县| 隆林| 翁牛特旗| 六安市| 庆元县| 夏津县| 辽中县| 温州市| 昔阳县| 舟山市| 沙河市| 德格县| 武夷山市| 平乐县| 南涧| 宜章县| 大港区| 化州市|