源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android注解ButterKnife的基本使用

  • 时间:2021-02-14 15:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android注解ButterKnife的基本使用
ButterKnife的最新版本是8.4.0。 首先,需要导入[code]ButterKnife[/code]的jar包。 在AndroidStudio中,[code]File->Project Structure->Dependencies->Library dependency [/code]搜索butterknife即可,第一个就是. 另外一种就是直接在build:grade(app)dependencies里添加:
compile 'com.jakewharton:butterknife:8.4.0' 
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 
ok,现在正式开始使用吧,用法也很简单 在Activity子类的[code]onCreate()[/code]方法里使用[code]ButterKnife.bind(this);[/code]即可
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),点击会出现: [img]http://files.jb51.net/file_images/article/201701/201701111418466.png[/img] 然后这样 [img]http://files.jb51.net/file_images/article/201701/201701111418467.png[/img] 选中的有TextView点击事件和findViewById的注解,点击Confirm就成功了! 什么,你说没有,别着急,你需要安装一个小插件(不要嫌麻烦,其实很简单,一劳永逸) 在[code]AndroidStudio->File->Settings->Plugins->[/code]搜索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的基本使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部