@InjectView -> @BindView @InjectViews -> @BindViews ButterKnife.inject(this) -> ButterKnife.bind(this) ButterKnife.reset(this) -> private Unbinder unbinder; unbinder = ButterKnife.bind(this, view); @Override public void onDestroyView() { unbinder.unbind(); super.onDestroyView(); }
compile 'com.jakewharton:butterknife:6.1.0'
mTextView1 = (TextView) findViewById(R.id.butter_text_view_1);
@InjectView(R.id.butter_text_view_2)
TextView mTextView2;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo_butter_knife); //using butter knife ButterKnife.inject(this); }
public class SimpleFragment extends Fragment { @InjectView(R.id.fragment_text_view) TextView mTextView; public SimpleFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_simple, container, false); ButterKnife.inject(this, view); mTextView.setText("TextView in Fragment are found!"); return view; } }
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.person_item_layout, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Person person = getItem(position); if (null != person) { holder.name.setText(person.getName()); holder.age.setText(String.valueOf(person.getAge())); holder.location.setText(person.getLocation()); holder.work.setText(person.getWork()); } return convertView; } static class ViewHolder { @InjectView(R.id.person_name) TextView name; @InjectView(R.id.person_age) TextView age; @InjectView(R.id.person_location) TextView location; @InjectView(R.id.person_work) TextView work; public ViewHolder(View view) { ButterKnife.inject(this, view); } }
finishButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } });
@OnClick(R.id.basic_finish_a_button) void finishA(View view) { finish(); }
//you can bind listener to multiple views @OnClick({R.id.button_enable, R.id.button_disable, R.id.button_alpha_0, R.id.button_alpha_1}) void editViewsClicked() { Toast.makeText(this, "You click the Button!", Toast.LENGTH_SHORT).show(); }
@InjectViews({R.id.label_first_name, R.id.label_middle_name, R.id.label_last_name}) ListlabelViews; @InjectViews({R.id.first_name, R.id.middle_name, R.id.last_name}) List nameViews;
public staticextends View> void apply(List list, Action super T> action) public static extends View, V> void apply(List list, Setter super T, V> setter, V value) public static extends View, V> void apply(List list, Property super T, V> setter, V value)
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> parent, View view, int position, long id) { } @Override public void onNothingSelected(AdapterView> parent) { } });
@OnItemSelected(R.id.my_spinner) //default callback : ITEM_SELECTED void onItemSelected(int position) { Toast.makeText(this, "position: " + position, Toast.LENGTH_SHORT).show(); }
@OnItemSelected(value = R.id.my_spinner, callback = OnItemSelected.Callback.NOTHING_SELECTED) void onNothingSelected() { Toast.makeText(this, "Nothing", Toast.LENGTH_SHORT).show(); }