public final class ButterKnife extends Object
Finding views from your activity is as easy as:
public class ExampleActivity extends Activity {
@Bind(R.id.title) EditText titleView;
@Bind(R.id.subtitle) EditText subtitleView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);
ButterKnife.bind(this);
}
}
Binding can be performed directly on an activity, a
view, or a dialog. Alternate objects to
bind can be specified along with an activity,
view, or
dialog.
Group multiple views together into a List
or array.
@Bind({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 bind 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 bindings.
If a view is optional add a @Nullable
annotation such as the one in the
support-annotations library.
@Nullable @Bind(R.id.title) TextView subtitleView;
Resources can also be bound to fields to simplify programmatically working with views:
@ResourceBool(R.bool.is_tablet) boolean isTablet;
@ResourceInt(R.int.columns) int columns;
@ResourceColor(R.color.error_red) int errorRed;
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.Setter<T extends android.view.View,V>
A setter that can apply a value to a list of views.
|
static interface |
ButterKnife.ViewBinder<T>
DO NOT USE: Exposed for generated code.
|
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 void |
bind(android.app.Activity target)
Bind annotated fields and methods in the specified
Activity . |
static void |
bind(android.app.Dialog target)
Bind annotated fields and methods in the specified
Dialog . |
static void |
bind(Object target,
android.app.Activity source)
Bind annotated fields and methods in the specified
target using the source
Activity as the view root. |
static void |
bind(Object target,
android.app.Dialog source)
Bind annotated fields and methods in the specified
target using the source
Dialog as the view root. |
static void |
bind(Object target,
android.view.View source)
Bind annotated fields and methods in the specified
target using the source
View as the view root. |
static void |
bind(android.view.View target)
Bind annotated fields and methods in the specified
View . |
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 |
setDebug(boolean debug)
Control whether debug logging is enabled.
|
static void |
unbind(Object target)
Reset fields annotated with
@Bind to null . |
public static void setDebug(boolean debug)
public static void bind(android.app.Activity target)
Activity
. The current content
view is used as the view root.target
- Target activity for view binding.public static void bind(android.view.View target)
View
. The view and its children
are used as the view root.target
- Target view for view binding.public static void bind(android.app.Dialog target)
Dialog
. The current content
view is used as the view root.target
- Target dialog for view binding.public static void bind(Object target, android.app.Activity source)
target
using the source
Activity
as the view root.target
- Target class for view binding.source
- Activity on which IDs will be looked up.public static void bind(Object target, android.view.View source)
target
using the source
View
as the view root.target
- Target class for view binding.source
- View root on which IDs will be looked up.public static void bind(Object target, android.app.Dialog source)
target
using the source
Dialog
as the view root.target
- Target class for view binding.source
- Dialog on which IDs will be looked up.public static void unbind(Object target)
@Bind
to null
.
This should only be used in the onDestroyView
method of a fragment.
target
- Target class for field unbind.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.