Andoid项目添加ButterKnife注解框架

项目地址

https://github.com/JakeWharton/butterknife

添加依赖

project 中build.gradle添加

buildscript {
    repositories {
        mavenCentral()  
    }
    dependencies {
        classpath'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

module中build.gradle添加

applyplugin:'com.neenbedankt.android-apt'

dependencies {
    compile'com.jakewharton:butterknife:8.0.1'
    apt'com.jakewharton:butterknife-compiler:8.0.1'
}

使用方法

http://jakewharton.github.io/butterknife/

Activity中

class ExampleActivity extends Activity {
    @BindView(R.id.title) TextView title;
    @BindView(R.id.subtitle) TextView subtitle; 
    @BindView(R.id.footer) TextView footer; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.simple_activity);
        ButterKnife.bind(this);
        // TODO Use fields... 
    }
}

Fragment中

public class FancyFragment extends Fragment {
    @BindView(R.id.button1) Button button1;
    @BindView(R.id.button2) Button button2;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fancy_fragment, container, false);
        ButterKnife.bind(this, view);
        // TODO Use fields...
        return view;
    }
}

ViewHolder中

static class ViewHolder {
    @BindView(R.id.title) TextView name;
    @BindView(R.id.job_title) TextView jobTitle;

    public ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}

其他用法

  • VIEW LISTS
  • LISTENER BINDING
  • BINDING RESET (Fragment中用)
  • OPTIONAL BINDINGS
  • MULTI-METHOD LISTENERS
  • BONUS

Android Studio插件

Android ButterKnife Zelezny

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,837评论 2 45
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,812评论 25 709
  • 十年的时间,未有这个人半点消息,可当他突然站在你面前时,这种心情怎能用语言描述。
    71640b15161f阅读 125评论 0 0
  • 学会发短信给我带来快乐 2010-06-13 16:46 阅读(31)评论(6) 我有一个手机,可是不常用。只要在...
    零星往事阅读 280评论 0 0
  • 刚看见罗胖公众号发表的《语言的饥荒和思维的贫困》,确实很受用,这个胖子虽然做商人做的吃相难看,偶尔露出的思辨方法也...
    然不是那个苒阅读 616评论 0 0

友情链接更多精彩内容