Eclipse使用Butterknife的相关配置

eclipse使用Butterknife的相关配置


Butterknife是Android一个注解的开源框架,使用简单,可以帮助我们初始化view、设置view监听等操作,省去了findViewById()提高开发的效率。

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

jar包下载:http://repo1.maven.org/maven2/com/jakewharton/butterknife/7.0.1/butterknife-7.0.1.jar


下面讲解一下在eclipse中如何使用butterknife:

步骤一:下载jar包,放到工程libs目录下,完成了这一步还不行,还要进行步骤二

步骤二:

(2.1)选择你的项目右键---->properties----->java compiler------>选中Annotation Processing------->勾选enable project specific settings

(2.2)展开Annotation Processing------>选中Factory Path ---->勾选enable project specific settings------>add JARs------>选中你的butterknife.jar

注意:如果步骤二没有Annotation Processing选项,可参照这篇文章解决:http://blog.csdn.net/lpforever/article/details/40779341


做完以上配置后就可以使用Butterknife了,但是要注意:属性布局不能用private or static 修饰,否则会报错


(1)在activity中使用

 public class MainActivity extends Activity 

{
    @Bind(R.id.tv_butt) TextView tvb;
    @Bind(R.id.tv_gutt) TextView tvg;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);//在这里初始化

     }

   @Override
    protected void onDestroy()
    {
        super.onDestroy();
        ButterKnife.unbind(this);//不要忘记在Activity或者Fragment销毁的时候调用
    }
}


(2)在Fragment中使用

public class MyFragment extends Fragment {   


    protected Context context; 
  
    @Nullable  
    @Override  
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        View view =inflater.inflate(R.layout.my_layout,container,false);  
        ButterKnife.bind(this,view);//绑定framgent  
        return view;  
    }   
  
    @Override  
    public void onDestroyView() {  
        super.onDestroyView();  
        ButterKnife.unbind(this);//解绑  
    }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值