转自http://www.cnblogs.com/mengdd/archive/2015/06/23/4595973.html
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}) List<TextView> labelViews; @InjectViews({R.id.first_name, R.id.middle_name, R.id.last_name}) List<EditText> nameViews;
public static <T extends View> void apply(List<T> list, Action<? super T> action) public static <T extends View, V> void apply(List<T> list, Setter<? super T, V> setter, V value) public static <T extends View, V> void apply(List<T> 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(); }