Butter Knife

转载出处:http://blog.youkuaiyun.com/zjbpku/article/details/18676149

Butter Knife也是使用注入视图的方式使开发人员尽可能少的编写代码。相比AndroidAnnotations,个人觉得没有AndroidAnnotations

使用简单,而且文档几乎也没有。还是先看一小段代码吧:

package com.example.butterknife;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.Button;  
import android.widget.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  
import butterknife.InjectView;  
import butterknife.OnClick;  
import butterknife.ButterKnife;  
import butterknife.OnItemClick;  
import butterknife.OnLongClick;  
  
import static android.widget.Toast.LENGTH_SHORT;  
  
public class SimpleActivity extends Activity {  
  @InjectView(R.id.title) TextView title;  
  @InjectView(R.id.subtitle) TextView subtitle;  
  @InjectView(R.id.hello) Button hello;  
  @InjectView(R.id.list_of_things) ListView listOfThings;  
  @InjectView(R.id.footer) TextView footer;  
  
  private SimpleAdapter adapter;  
  
  @OnClick(R.id.hello) void sayHello() {  
    Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();  
  }  
  
  @OnLongClick(R.id.hello) boolean sayGetOffMe() {  
    Toast.makeText(this, "Let go of me!", LENGTH_SHORT).show();  
    return true;  
  }  
  
  @Override protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.simple_activity);  
    ButterKnife.inject(this);  
  
    // Contrived code to use the "injected" views.  
    title.setText("Butter Knife");  
    subtitle.setText("View \"injection\" for Android.");  
    footer.setText("by Jake Wharton");  
    hello.setText("Say Hello");  
  }  
}  
总之,两者虽有相似的地方,但看到AndroidAnnotations之后,就不愿使用Butter Knife了。另外,值得你注意的是butter Knife

有个代码生成的插件呢,不过是针对Android Studio的,请看图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值