AndroidStudio中使用ButterKnife教程!

转自:http://blog.csdn.net/true100/article/details/51801936
之前了解和使用过另外一个比较流行的Android注解开源框架AndroidAnnotions,其功能也非常强大,使用比较方便,唯一的不足就是要在项目
AndroidManifest.xml中为Activity配置对应的Activity_类。我们把AndroidAnnotions和ButterKnife简单地比较下:
  • AndroidAnnotions利用注解方式简化代码,提高开发效率,但是相对于ButterKnife配置麻烦点。其通过反射机制注解,占用内存资源相对较多,也比较耗时。
  • ButterKnife配置和使用都非常方便,通过View注入绑定进行注解提升开发效率,运行时也不会影响APP效率。

    ButterKnife开源地:https://github.com/JakeWharton/butterknife,现在最新版本是8.1.0。

—-第一步:AndroidStudio中配置ButterKnife环境—— 
1,在项目mode对应的.gradle文件顶部中添加:

apply plugin: 'com.neenbedankt.android-apt'
 
 
  • 1
  • 1

及在dependencies{}中添加:

    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    compile 'com.jakewharton:butterknife:8.0.1'
 
 
  • 1
  • 2
  • 1
  • 2

我项目的配置如下:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.ldm.bufferknife"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    compile 'com.jakewharton:butterknife:8.0.1'
    compile 'com.android.support:appcompat-v7:24.0.0'
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

2,在工程中的.gradle文件中dependencies {}添加classpath ‘com.neenbedankt.gradle.plugins:Android-apt:1.8’,结果如下:

 dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

之所以要这样配置,是因为运行ButterKnif最新版本8.1.0时,会报空指针异常,我也是找了很久原因,后来参考博客:http://blog.csdn.net/blueamertj/article/details/51517191才解决问题的。

—注解代码之:Activity中使用注解——-

/**
 * @param
 * @author ldm
 * @description Android注解框架:ButterKnife使用简介
 * @time 2016/7/4 9:07
 */
public class MainActivity extends Activity {
    //通过@BindView()绑定控件。以前版本用@InjectView()来绑定控件,现在是用@BindView绑定控件
    @BindView(R.id.buk_textview)
    TextView buk_textview;
    @BindView(R.id.buk_btn)
    Button buk_btn;
    @BindView(R.id.buk_listview)
    ListView buk_listview;
    private List<DataBean> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //以前用的是ButterKnife.inject()来绑定Activity,现在用的是ButterKnife.bind(),版本不同相应的方法不同
        ButterKnife.bind(MainActivity.this);
        //接下来就可以直接使用我们已经绑定好的控件
        initListViewData();
    }

    private void initListViewData() {
        datas = new ArrayList<>();
        for (int i = 0; i <= 6; i++) {
            DataBean bean = new DataBean();
            bean.setTitle("这是标题" + i);
            bean.setContent("内容暂时都一样吧");
            datas.add(bean);
        }
        buk_listview.setAdapter(new TestAdapter(this, datas));
    }

    //通过ButterKnife对控件的监听事件进行处理
    //注解绑定Button的点击事件
    @OnClick(R.id.buk_btn)
    public void buttonClick() {
        Toast.makeText(this, "不许再点我!", Toast.LENGTH_SHORT).show();
    }

    //ListView的item点击事件
    @OnItemClick(R.id.buk_listview)
    public void onitemClick(int pos) {//点击事件
        Toast.makeText(this, "你点击的是第" + pos + "条数据", Toast.LENGTH_SHORT).show();
    }

    //ListView的item长按监听事件
    @OnItemLongClick(R.id.buk_listview)
    public boolean onLongClick(View view) {//注意:这个方法返回boolean类型
        Toast.makeText(this, "你要不要再按久点!", Toast.LENGTH_SHORT).show();
        return true;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

—–注解代码之:Adapter中使用注解—–

/**
 * description:Adapter中使用ButterKnife注解
 * 作者:ldm
 * 时间:20162016/7/4 10:25
 */
public class TestAdapter extends BaseAdapter {
    private Context mContext;
    private List<DataBean> datas;

    public TestAdapter(Context mContext, List<DataBean> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.item_title.setText(datas.get(i).getTitle());
        holder.item_content.setText(datas.get(i).getContent());
        return view;
    }

    class ViewHolder {
        @BindView(R.id.item_title)//在Adapter中通过注解绑定控件
                TextView item_title;
        @BindView(R.id.item_content)
        TextView item_content;

        public ViewHolder(View v) {
            ButterKnife.bind(this, v);//注解绑定View
        }
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

—-i测试数据用到的实体类——

/**
 * description:
 * 作者:ldm
 * 时间:20162016/7/4 10:29
 */
public class DataBean implements Serializable {
    private String title;
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

使用中发现了几个小注意点: 
1. 注入的控件View或事件注入比如 onclick时,变量或方法的修饰符不能是private 和 static的。 
2. 比如ListView的长按事件@OnLongClick对应的方法返回是boolean类型,即不同的方法写法是不完全相同的。 
3. 我们在adapter中使用注解时,一般是在ViewHolder的构造方法中传入View对象,并且要调用ButterKnife.bind(this, v)方法绑定对应View。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值