View注入框架:Butterknife简单使用
1.Activity Binging
class ExampleActivity extends Activity { @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // TODO Use fields... }} |
替换了缓慢的反射,通过自己主动生成代码来运行查询。你能够看到或者Debug调用@Bind的对象来生成代码。下图为上图所生成的代码。
public void bind(ExampleActivity activity) { activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578); activity.footer = (android.widget.TextView) activity.findViewById(2130968579); activity.title = (android.widget.TextView) activity.findViewById(2130968577);} |
2.Non Activity Binging
public class FancyFragment extends Fragment { @Bind(R.id.button1) Button button1; @Bind(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; }} |
3.Use in Adapter
public class MyAdapter extends BaseAdapter { @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("John Doe"); // etc... return view; } static class ViewHolder { @Bind(R.id.title) TextView name; @Bind(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.bind(this, view); } }} |
4.View List
你能够将多个View分组存储到List或者Array中
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })List<EditText> nameViews;The apply method allows you to act on all the views in a list at once.ButterKnife.apply(nameViews, DISABLE);ButterKnife.apply(nameViews, ENABLED, false);Action and Setter interfaces allow specifying simple behavior.static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() { @Override public void apply(View view, int index) { view.setEnabled(false); }};static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() { @Override public void set(View view, Boolean value, int index) { view.setEnabled(value); }}; |
一个Android属性相同能够使用apply 方法
ButterKnife.apply(nameViews, View.ALPHA, 0.0f); |
5.Listener Binging
Listeners也能够自己主动配置方法
@OnClick(R.id.submit)public void submit(View view) { // TODO submit data to server...} |
@OnClick(R.id.submit)public void submit() { // TODO submit data to server...} |
@OnClick(R.id.submit)public void sayHi(Button button) { button.setText("Hello!");} |
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "Try again", LENGTH_SHORT).show(); }} |
自己定义视图能够绑定自己的事件监听而且不指定ID
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! }} |
6.Binging Reset
Fragments 比Activity有不同的View生命周期,在Fragmrnt 的onCreateView方法中绑定,onDestroyView方法中销毁Views。Butter
Knife有unbind 方法自己主动去做。
public class FancyFragment extends Fragment { @Bind(R.id.button1) Button button1; @Bind(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.unbind(this); }} |
7.Optional Binging
默认情况下,@Bind和侦听器绑定都是必需的。假设目标View无法找到它将抛出一个异常。
@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;@Nullable @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() { // TODO ...} |
8.Multi-Method Listeners
方法凝视对应的监听器有多个回调能够绑定到不论什么当中之中的一个。每个凝视都绑定到一个默认的回调。使用callback 參数指定一个替代
|
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
|
9.Bonus
还包括findById方法的简化依旧须要知道Views在哪个view、Activity、Dialog。
它使用泛型来判断返回类型,并自己主动运行。
View view = LayoutInflater.from(context).inflate(R.layout.thing, null);TextView firstName = ButterKnife.findById(view, R.id.first_name);TextView lastName = ButterKnife.findById(view, R.id.last_name);ImageView photo = ButterKnife.findById(view, R.id.photo); |
compile 'com.jakewharton:butterknife:(insert latest version)' |
-keep class butterknife.** { *; }
|
View注入框架:Butterknife简单使用的更多相关文章
- [Android] Android 注解绑定UI View组件库 ButterKnife 的使用
ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤.是大神JakeW ...
- ButterKnife--View注入框架的使用
作为一名Android开发,是不是经常厌烦了大量的findViewById以及setOnClickListener代码,而ButterKnife是一个专注于Android系统的View注入框架,让你从 ...
- ButterKnife--View注入框架
俗话说,不会偷懒的程序员不是好程序员!作为一名Android的开发者,我们已经厌烦了经常写大量的findViewById以及setOnClickListener代码.而ButterKnife是一个专注 ...
- ButterKnife View 注入
/***************************************************************************************** * ButterK ...
- 玩转ButterKnife注入框架
在去年这个时候,我写过一篇介绍Android注解的文章android注解使用详解,这篇文章主要是介绍了Android中的AndroidAnnotations注入框架,AA框架有它自身的一些优点,这里不 ...
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...
- 【二】注入框架RoboGuice使用:(Your First View Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([一]注入框架RoboGuice使用:(A brief example of what RoboGuice does)),今天我们我看下View的注 ...
- 简单谈谈Hilt——依赖注入框架
今天继续Jetpack专题,相信不少的朋友都使用过Dagger,也放弃过Dagger,因为实在太难用了.所以官方也是为了让我们更好使用依赖注入框架,为我们封装了一个新的框架--Hilt,今天一起来看看 ...
- Butter Knife:一个安卓视图注入框架
Butter Knife:一个安卓视图注入框架 2014年5月8日 星期四 14:52 官网: http://jakewharton.github.io/butterknife/ GitHub地址: ...
随机推荐
- 小学生绞尽脑汁也学不会的python(反射)
小学生绞尽脑汁也学不会的python(反射) 1. issubclass, type, isinstance issubclass 判断xxxx类是否是xxxx类的子类 type 给出xxx的数据类型 ...
- linux环境下删除包含特殊字符的文件或目录
linux环境下删除包含特殊字符的文件或目录 ls -liUse find command as follows to delete the file if the file has inode nu ...
- Docker可视化管理工具对比(DockerUI、Shipyard、Rancher、Portainer)
1.前言 谈及docker,避免不了需要熟练的记住好多命令及其用法,对于熟悉shell.技术开发人员而言,还是可以接受的,熟练之后,命令行毕竟是很方便的,便于操作及脚本化.但对于命令行过敏.非技术人员 ...
- DebugBar v7.0.2 注册码
blog.sina.com.cn/seoerx 14d4fb95f89bdd277fff0d20910be400 seoerx.diandian.com 505dc8424062f9895c2dd14 ...
- [AngularJS]Chapter 4 AngularJS程序案例分析
前边讲的都是基础.本章看看他们怎么合作的. 我们要建一个程序.一次一步.章末结束 [这个程序] GutHub是一个简单的菜谱管理程序.功能是存好吃的的菜谱并提供步骤.这个程序包含: 两列布局 左边是导 ...
- HDU 4035
dp求期望的题. 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望.E[1]即为所求. 叶子结点: E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i] ...
- php学习之道:php empty()和isset()的差别
在使用 php 编写页面程序时,我常常使用变量处理函数推断 php 页面尾部參数的某个变量值是否为空,開始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...
- 电脑显示U盘,可是读取不了
问题: 我的一个内存卡没用,放到了读卡器上.刚開始能用,可是到了后来,突然之间发现: 插入读卡器之后,仅仅是在U下角显示有有U盘提示,提示"打开设备和打印"或者"安全删除 ...
- JDBC连接mysql时出现的ssl问题
使用MySQL数据库时出现如下错误: WARN: Establishing SSL connection without server's identity verification is not r ...
- 2)Win10-UWA开发 API參考 - 1
孙广东 2015.8.23 大多数 Windows 执行时 API 如今适用于 Windows Phone 应用商店应用以及 Windows 应用商店应用,这意味着当你创建同一时候面向 Window ...