Butter Knife框架的基本使用

写这篇博客主要是因为看到下面的代码感觉一阵的恶心:
关于Butter Knife的介绍地址http://jakewharton.github.io/butterknife/

lvRoom = rootView.findViewById(R.id.lv_room);
        tvGradeOne = rootView.findViewById(R.id.grade_one1);
        tvGradeTwo = rootView.findViewById(R.id.grade_two1);
        tvGradeThree = rootView.findViewById(R.id.grade_three1);
        tvGradeFour = rootView.findViewById(R.id.grade_four1);
        tvGradeFive = rootView.findViewById(R.id.grade_five1);
        tvGradeSix = rootView.findViewById(R.id.grade_six1);
        ivBack = rootView.findViewById(R.id.iv_back1);
        iv1 = rootView.findViewById(R.id.ib1);
        iv2 = rootView.findViewById(R.id.ib2);
        iv3 = rootView.findViewById(R.id.ib3);
        iv4 = rootView.findViewById(R.id.ib4);
        iv5 = rootView.findViewById(R.id.ib5);
        iv6 = rootView.findViewById(R.id.ib6);
        ivCreateRoom = rootView.findViewById(R.id.iv_create_room);
        ivFindRoom = rootView.findViewById(R.id.iv_find_room);
        ivFastJoin = rootView.findViewById(R.id.iv_fast_join);
        tvGradeOne.setOnClickListener(this);
        tvGradeTwo.setOnClickListener(this);
        tvGradeThree.setOnClickListener(this);
        tvGradeFour.setOnClickListener(this);
        tvGradeFive.setOnClickListener(this);
        tvGradeSix.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        iv5.setOnClickListener(this);
        iv6.setOnClickListener(this);
        ivCreateRoom.setOnClickListener(this);
        ivFastJoin.setOnClickListener(this);
        ivFindRoom.setOnClickListener(this);

相信大家看到这种代码都会感觉一阵恶心吧(嘿嘿,但是这又是省不了的)
所以遇到这样的能咋办呢。所以就可以用到Butter Knife框架了。

使用方法先把依赖加上:

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

然后布局的时候该怎么布局还是怎么布局,只不过声明控件的时候不需要使用findviewbyid了,直接使用

@BindView(R.id.textView4) MyTextView tv4;

这样也就声明了一个view

点击事件的话直接用

@OnClick(R.id.textView4)
    public void click(View v) {
        Toast.makeText(getActivity(), "sss", Toast.LENGTH_SHORT).show();
    }

这样也就给该控件配置了监听器

还有各种监听的使用,在这里我就不多提了依葫芦画瓢就行了。

ButterKnife.bind(this, v);
最容易忘记的就是这句代码了,在初始化的时候加上这句代码就OK了。
在activity中调用ButterKnife.bind(this);
在Fragment中调用ButterKnife.bind(this, v);

然后你就会发现那些恶心的代码没有了 看起来也顺眼多了(嘿嘿!!!)

下面是代码:

public class Fragment8 extends Fragment {
    @BindView(R.id.textView4)
    MyTextView tv4;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment8, container, false);
        ButterKnife.bind(this, v);
        return v;
    }

    @OnClick(R.id.textView4)
    public void click() {
        Toast.makeText(getActivity(), "sss", Toast.LENGTH_SHORT).show();
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mrl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.dgg1.demo.MyRelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="#00ff00">

        <com.dgg1.demo.MyTextView
            android:id="@+id/textView4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#fff000" />
    </com.dgg1.demo.MyRelativeLayout>
</RelativeLayout>

如有错误请留言

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值