资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ButterKnife8.5.1怎么用-创新互联

这篇文章主要为大家展示了“ButterKnife 8.5.1怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ButterKnife 8.5.1怎么用”这篇文章吧。

创新互联专注于长岭企业网站建设,自适应网站建设,电子商务商城网站建设。长岭网站建设公司,为长岭等地区提供建站服务。全流程定制制作,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

ButterKnife 简介

  ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码。在7.0版本以后引入了注解处理器,取代了之前利用反射原理进行findViewById影响APP性能的实现方式,不再影响APP运行效率。

使用须知:

1.Activity中使用ButterKnife.bind(this);必须在setContentView();之后,父类中bind后,子类不需要再bind

2.Fragment中使用ButterKnife.bind(this, rootView); 根布局View对象,需要在onDestroyView中解绑

3.注解的属性不能用private或static修饰,否则会报错

4.Activity官方例子中没有解绑操作,而Fragment需要解绑

使用步骤:

1、添加依赖

  compile 'com.jakewharton:butterknife:8.5.1'

  annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

备注:只需要添加上述依赖即可使用。

只有当你在Library中使用ButterKnife时,才需要如下步骤:

1、在Project的build.gradle中添加:

classpath'com.jakewharton:butterknife-gradle-plugin:8.5.1'

2、在对应Library(Module)中添加:

apply plugin:'com.jakewharton.butterknife'

网上许多的使用文章都把上述两个步骤当做是必须的。非也!

(注意:To use ButterKnife in a library)

实际项目

实际项目中一般都会有定义一个BaseActivity或BaseFragment,在基类中绑定,则在其所有的子类中都可以直接使用。需要注意的是,父类中需要在子类的setContentView调用完后再绑定。

使用完整范例

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.textview)
    TextView textview;
    @BindView(R.id.button)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_new);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.textview, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textview:
                break;
            case R.id.button:
                break;
        }
    }
}

@BindView(R.id.textview)//声明并绑定变量
TextView textview;
//相当于:TextViewtextView = (TextView) findViewById(R.id.textView);
 
@OnClick(R.id.button)//给按钮绑定方法,可以不声明引用变量
public void onViewClicked() {
}
 
@BindViews({R.id.button1, R.id.button2, R.id.button3})
public List

在fragment中使用(需要解绑)

private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_regist_create, container, false);
    unbinder = ButterKnife.bind(this, view);//需要加多一个加载了布局的View对象
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();//fragment需要解绑
}

再偷懒一点

Android Studio插件:Android Butterknife Zelezny

方便给整个布局文件一键生成注解。

使用方法:

  在setContentView(R.layout.activity_main)中对着activity_main右键,generate,Generate ButterKnife Injections,勾选需要生成注解的控件即可

ButterKnife 8.5.1怎么用

图片来自android-butterknife-zelezny

总结

一般我认为简化了findViewById和setOnClickListener之后,已经很方便了。并不会去使用ButterKnife中方方面面的功能,在多控件的界面例如提交表单数据时非常好用。这也就是“80/20”法则在生效吧!对于使用比较少的其他特性如解除绑定等,用到之时再尝试即可!

参考文档

[Android开发] ButterKnife8.5.1 使用方法教程总结

同类型框架参考

google/dagger

https://github.com/google/dagger

androidannotations

https://github.com/androidannotations/androidannotations

附录:

注解类型

* @BindViews

* @BindView

* @BindArray

* @BindBitmap

* @BindBool

* @BindColor

* @BindDimen

* @BindDrawable

* @BindFloat

* @BindInt

* @BindString

事件类型

* @OnClick

* @OnCheckedChanged

* @OnEditorAction

* @OnFocusChange

* @OnItemClick item

* @OnItemLongClick

* @OnItemSelected

* @OnLongClick

* @OnPageChange

* @OnTextChanged

* @OnTouch

* @Optional

===================================================================================

Github上的使用说明:

Download

dependencies {

 compile 'com.jakewharton:butterknife:8.5.1'

 annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

}

Snapshots of thedevelopment version are available in Sonatype's snapshots repository.

Library projects

To use ButterKnife in a library, add the plugin to your buildscript:

buildscript {

 repositories {

   mavenCentral()

  }

 dependencies {

   classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

 }

}

and then apply it in your module:

  apply plugin: 'com.android.library'

  apply plugin: 'com.jakewharton.butterknife'

Now make sureyou use R2 instead of R inside allButter Knife annotations.

classExampleActivityextendsActivity {

   @BindView(R2.id.user) EditText username;

   @BindView(R2.id.pass) EditText password;

...

}

以上是“ButterKnife 8.5.1怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:ButterKnife8.5.1怎么用-创新互联
转载来源:http://cdkjz.cn/article/dhiooo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220