学习ButterKnife 8.4.0
来源:互联网 发布:社会关系网络问卷 编辑:程序博客网 时间:2024/06/06 00:43
ButterKnife的GitHub: https://github.com/JakeWharton/butterknife
参考博客:
项目添加 http://blog.csdn.net/skeeing/article/details/52384025
详细使用说明 http://blog.csdn.net/skeeing/article/details/52386438
本文是以ButterKnife 8.4.0为基础,记录学习的过程,如果有错误,求指正。
一. 添加到项目
在app的build.gradle中添加以下依赖就可以
dependencies { compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'}
二. 使用ButterKnife
在activity中使用需要绑定, 建议在BaseActivity中绑定
注意:在8.4.0中已经不需要解绑了 ButterKnife.unbind()
注意:ButterKnife.bind()
需要在setContentView()
之后进行绑定
再让MainActivity 继承自BaseActivity,接下来修改布局MainActivity的布局来练习ButterKnife的使用
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.panji.studybutterknife.MainActivity"> <TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/bt_1" android:layout_width="match_parent" android:layout_height="50dp" android:text="这是按钮1"/> <Button android:id="@+id/bt_2" android:layout_width="match_parent" android:layout_height="50dp" android:text="这是按钮2"/> <Button android:id="@+id/bt_goto_second_activity" android:layout_width="match_parent" android:layout_height="50dp" android:text="跳到第二页"/> <EditText android:id="@+id/et_show" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="显示区"/></LinearLayout>
回到MainActivity中输入@BindView(R.id.tv_hello) TextView tvHello;
再在init()方法中tvHello.setText("使用黄油刀");
在这个时候,如果你运行一下,你就会发现TextView已经被显示出来了。接下来,可以从这个点延伸出去,学习ButterKnife。
以下是MainActivity:
public class MainActivity extends BaseActivity{ @BindView(R.id.tv_hello) TextView tvHello; @BindView(R.id.et_show) EditText etShow; @Override public int getContentViewId() { return R.layout.activity_main; } @Override protected void init(Bundle savedInstanceState) { tvHello.setText("改变"); } @OnClick({R.id.bt_1, R.id.bt_2, R.id.bt_goto_second_activity}) public void onClickBt(View view){ switch (view.getId()){ case R.id.bt_1: etShow.setText("1"); break; case R.id.bt_2: etShow.setText("2"); break; case R.id.bt_goto_second_activity: Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); break; default: break; } }}
对于Button你甚至不需要去BindView就能编写点击事件
不绑定View
@OnClickpublic void 方法名(View view){}
绑定View
@OnClick(x.x.x)public void 方法名(View view){}
对多个Button可以写在一个方法中,在方法中判断点击的是哪个按钮
@OnClick({x.x.x, x.x.x, x.x.x})public void 方法名(View view){}
除了点击事件外还有其他的事件也是差不多的用法,比如第二个Activity中的ListView的@OnItemClick,具体的使用请大家自己尝试
注意:RecyclerView不能使用@OnItemClick来使用它的item点击事件,你可以使用原来的方法,自己写或用一些大神的封装。
对RecyclerView.ViewHolder
的也是可以使用ButterKnife的,比如
public class MyHolderView extends RecyclerView.ViewHolder{ private IMyViewHolderClicks mListener; public interface IMyViewHolderClicks{ void OnItemClickHolder(int pos); } @BindView(android.R.id.text1) TextView text1; public MyHolderView(View itemView, IMyViewHolderClicks itemClickListener) { super(itemView); mListener = itemClickListener; ButterKnife.bind(this, itemView); } @OnClick public void OnClick(View view){ mListener.OnItemClickHolder(getLayoutPosition()); }}
Demo中的第三个Activity练习了ButterKnife在RecyclerView的使用。
END
DEMO下载
- 学习ButterKnife 8.4.0
- butterknife 8.4.0
- ButterKnife 8.4.0
- Android Butterknife 8.4.0
- ButterKnife学习
- ButterKnife学习
- ButterKnife学习
- 【butterknife】butterknife:8.4.0 黄油刀使用详解。
- ButterKnife:8.4.0的使用
- 黄油刀 Butterknife 8.4.0
- Butterknife 8.4.0 使用方法总结
- Butterknife使用(学习总结)
- butterKnife原理学习
- 学习随笔:butterknife、Executors
- Android ButterKnife框架学习
- Butterknife源码学习
- Android ButterKnife学习总结
- butterknife 8.4.0 android studio 配置
- EventBus_事件传递框架
- 斯坦福机器学习公开课6-x 使用逻辑回归处理多分类
- iOS 长截图开发
- 对象池——Smiple Pool For Unity
- 史上最全的机器学习资料
- 学习ButterKnife 8.4.0
- TypeScript 错误“property does not exist on value of type”
- CFont::CreateFont 详解
- PLSQL无法查看所有的表
- Http通信访问网络资源(Get与Post请求)
- 仿饿了么“谁去拿外卖”代码
- 标准函数库(#include<stdio.h>)
- 模拟聊天的jar包
- srs压力测试