软件框架之ButterKnife的使用

news/2024/7/15 16:43:03
1、简介
ButterKnife是注解中相对简单易懂的很不错的开源框架
1.强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2.方便的处理Adapter里的ViewHolder绑定问题
3.运行时不会影响APP效率,使用配置方便
4.代码清晰,可读性强
2、下载地址
https://github.com/JakeWharton/butterknife
3、使用步骤
1)在Setting->Plugins中输入butterknife添加插件
Android ButterKnife Aelezny ->点击安装
2)Module里的build.gradle里面添加
compile 'com.jakewharton:butterknife:7.0.1'
4、常用功能
本文主要针对7.0.1版本
1)省略findViewById()
(1)Activity中使用
以前
mTextView1 = (TextView) findViewById(R.id.butter_text_view_1);
现在
@InjectView(R.id.butter_text_view_2)
TextView mTextView2;
在设置好布局之后调用:ButterKnife.inject(this);:
注意
View变量声明的时候不能为private或者static.
(2)Fragment中使用
View view = inflater.inflate(R.layout.fragment_simple, container, false);
        ButterKnife.inject(this, view);
@InjectView(R.id.fragment_text_view)
    TextView mTextView;
mTextView.setText("TextView in Fragment are found!");
(3)Adapter ViewHolder中使用
static class ViewHolder {
        @InjectView(R.id.person_name)
        TextView name;
 
        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
2)省略setOnClickListener()
以前
finishButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
现在
@OnClick(R.id.basic_finish_a_button)
    void finishA(View view) {
        finish();
    }
ButterKnife.inject(this);
注意
注意这里方法仍然不能是private和static, 并且可以有一个参数View,也可不写.
3)ListView的点击@OnItemClick, CheckBox的@OnCheckedChanged也可以实现省略操作
4)可以一次指定多个id,为多个View绑定一个事件处理方法
@OnClick({R.id.button_enable, R.id.button_disable, R.id.button_alpha_0, R.id.button_alpha_1})
void editViewsClicked() {
    Toast.makeText(this, "You click the Button!", Toast.LENGTH_SHORT).show();



http://www.niftyadmin.cn/n/3649334.html

相关文章

python pyenv_如何使用Pyenv和Direnv管理Python

python pyenv介绍 (Introduction) Whether you’re just getting started or you’re a seasoned Python developer, you may have found managing your Python environments to be tedious and painful. Managing Python versions, libraries, and various dependencies is li…

不能一帆风顺,那就乘风破浪

我们这一生很短,我们终将会失去它。所以不妨大胆一点,爱一个人,攀一座山,追一次梦。不妨大胆一点,有很多事都没有答案。 – 《大鱼海棠》 ▣ 博主主站地址:微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&am…

软件框架之EventBus的使用

1、简介EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都…

[dotNET]如何启用WSE2.0的强大的Trace功能

如何启用WSE2.0的Trace功能摘要&#xff1a;郑昀UltraPower 简单介绍了使用了WSE2.0的应用程序如何启用WSE2.0自身的Trace。给我们的应用的“App.exe.config”配置文件添加下面的粗字体&#xff1a;<configSections><section name"microsoft.web.services2"…

prisma orm_Prisma中的身份验证-第2部分:JSON Web令牌和登录

prisma ormOver in Part 1 we setup our project to require authentication to interact with the GraphQL API. Now, we’re going to look at logging in users and generating JSON Web Tokens (JWT) for our users to lock them out from data we don’t want them to acc…

“微笑涛声”微信公众号正式上线运行

2020年2月2日&#xff0c;微笑涛声个人博客上线&#xff0c;经过一年多的更新&#xff0c;写了原创文章125篇&#xff0c;访问达到8万次以上&#xff0c;百度收录500&#xff0c;自己也成长了许多。 ▣ 博主主站地址&#xff1a;微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&a…

angular 模块构建_使用传单在Angular中构建地图,第4部分:形状服务

angular 模块构建By now, we’ve built a Leaflet map in Angular and we are able to render markers and pop-ups. Let’s now render shapes for the different US states. 到目前为止 &#xff0c;我们已经在Angular中构建了Leaflet地图 &#xff0c;并且能够渲染标记和弹出…