public final class ButterKnife extends Object
Injecting views from your activity is as easy as:
public class ExampleActivity extends Activity {
@InjectView(R.id.title) EditText titleView;
@InjectView(R.id.subtitle) EditText subtitleView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);
ButterKnife.inject(this);
}
}
Injection can be performed directly on an activity, a
view, or a dialog. Alternate objects to
inject can be specified along with an activity,
view, or
dialog.
Group multiple views together into a List
or array.
@InjectViews({R.id.first_name, R.id.middle_name, R.id.last_name})
List nameViews;
There are three convenience methods for working with view collections:
apply(List, Action)
– Applies an action to each view.apply(List, Setter, Object)
– Applies a setter value to each view.apply(List, Property, Object)
– Applies a property value to each view.To inject listeners to your views you can annotate your methods:
@OnClick(R.id.submit) void onSubmit() {
// React to button click.
}
Any number of parameters from the listener may be used on the method.
@OnItemClick(R.id.tweet_list) void onTweetClicked(int position) {
// React to tweet click.
}
Be default, views are required to be present in the layout for both field and method injections.
If a view is optional add the @Optional
annotation.
@Optional @InjectView(R.id.title) TextView subtitleView;
Modifier and Type | Class and Description |
---|---|
static interface |
ButterKnife.Action<T extends android.view.View>
An action that can be applied to a list of views.
|
static class |
ButterKnife.Finder
DO NOT USE: Exposed for generated code.
|
static interface |
ButterKnife.Injector<T>
DO NOT USE: Exposed for generated code.
|
static interface |
ButterKnife.Setter<T extends android.view.View,V>
A setter that can apply a value to a list of views.
|
Modifier and Type | Method and Description |
---|---|
static <T extends android.view.View> |
apply(List<T> list,
ButterKnife.Action<? super T> action)
Apply the specified
action across the list of views. |
static <T extends android.view.View,V> |
apply(List<T> list,
ButterKnife.Setter<? super T,V> setter,
V value)
Set the
value using the specified setter across the list of views. |
static <T extends android.view.View,V> |
apply(List<T> list,
android.util.Property<? super T,V> setter,
V value)
Apply the specified
value across the list of views using the property . |
static <T extends android.view.View> |
findById(android.app.Activity activity,
int id)
Simpler version of
Activity.findViewById(int) which infers the target type. |
static <T extends android.view.View> |
findById(android.app.Dialog dialog,
int id)
Simpler version of
Dialog.findViewById(int) which infers the target type. |
static <T extends android.view.View> |
findById(android.view.View view,
int id)
Simpler version of
View.findViewById(int) which infers the target type. |
static void |
inject(android.app.Activity target)
Inject annotated fields and methods in the specified
Activity . |
static void |
inject(android.app.Dialog target)
Inject annotated fields and methods in the specified
Dialog . |
static void |
inject(Object target,
android.app.Activity source)
Inject annotated fields and methods in the specified
target using the source
Activity as the view root. |
static void |
inject(Object target,
android.app.Dialog source)
Inject annotated fields and methods in the specified
target using the source
Dialog as the view root. |
static void |
inject(Object target,
android.view.View source)
Inject annotated fields and methods in the specified
target using the source
View as the view root. |
static void |
inject(android.view.View target)
Inject annotated fields and methods in the specified
View . |
static void |
reset(Object target)
|
static void |
setDebug(boolean debug)
Control whether debug logging is enabled.
|
public static void setDebug(boolean debug)
public static void inject(android.app.Activity target)
Activity
. The current content
view is used as the view root.target
- Target activity for field injection.public static void inject(android.view.View target)
View
. The view and its children
are used as the view root.target
- Target view for field injection.public static void inject(android.app.Dialog target)
Dialog
. The current content
view is used as the view root.target
- Target dialog for field injection.public static void inject(Object target, android.app.Activity source)
target
using the source
Activity
as the view root.target
- Target class for field injection.source
- Activity on which IDs will be looked up.public static void inject(Object target, android.view.View source)
target
using the source
View
as the view root.target
- Target class for field injection.source
- View root on which IDs will be looked up.public static void inject(Object target, android.app.Dialog source)
target
using the source
Dialog
as the view root.target
- Target class for field injection.source
- Dialog on which IDs will be looked up.public static void reset(Object target)
@InjectView
and @InjectViews
to null
.
This should only be used in the onDestroyView
method of a fragment.
target
- Target class for field reset.public static <T extends android.view.View> void apply(List<T> list, ButterKnife.Action<? super T> action)
action
across the list
of views.public static <T extends android.view.View,V> void apply(List<T> list, ButterKnife.Setter<? super T,V> setter, V value)
value
using the specified setter
across the list
of views.public static <T extends android.view.View,V> void apply(List<T> list, android.util.Property<? super T,V> setter, V value)
value
across the list
of views using the property
.public static <T extends android.view.View> T findById(android.view.View view, int id)
View.findViewById(int)
which infers the target type.public static <T extends android.view.View> T findById(android.app.Activity activity, int id)
Activity.findViewById(int)
which infers the target type.public static <T extends android.view.View> T findById(android.app.Dialog dialog, int id)
Dialog.findViewById(int)
which infers the target type.Copyright © 2013-2015. All Rights Reserved.