- @BindView findViewById()
ButterKnife.bind(this, view);
@BindView(R.id.user) EditText username;
//activity
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(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...
}
}
//non-activity
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.frag, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
//Controller
ButterKnife.bind(this, activity)
//
merge inflate之后 或 onFinishInflate()
- @BindString 资源
@BindString(R.string.login_error) String loginErrorMessage;
@BindBool、@BindColor、@BindDimen、@BindDrawable、@BindInt、@BindString
- @BindViews 将多个views绑定到数组
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
//在这个列表中每一个元素上进行调用
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
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);
}
};
- @OnClick 监听器绑定
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
//All arguments to the listener method are optional.
//参数可选
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
//Define a specific type and it will automatically be cast.
//参数类型识别
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
//Specify multiple IDs in a single binding for common event handling.
//绑定多个id控件到同一个监听器上
@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();
}
}
//Custom views can bind to their own listeners by not specifying an ID.
//自定义View绑定事件监听时无需ID:
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
- unbind Fragments
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
- 多回调方法
//能够对监听器任何一个函数进行绑定
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
//每一个注解都会绑定到一个默认的回调。可以指定callback参数
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
- @Nullable 忽视异常
@Nullable
@BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional
@OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
- findById 免转换
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);