ButterKnife--View注入框架

Github 地址: html

https://github.com/hpu-spring87/butterknifeandroid

 

http://www.stormzhang.com/openandroid/android/2014/01/12/android-butterknife/git

 

俗話說:「不會偷懶的程序員不是好的程序員!」。做爲一名Android開發,是否是常常厭煩了大量的findViewById以及setOnClickListener代碼,而ButterKnife是一個專一於Android系統的View注入框架,讓你今後從這些煩人臃腫的代碼中解脫出來。先來看一段代碼示例說明下ButterKnife是如何簡化代碼的:程序員

注意:若是你是使用的Eclipse引用該library,你須要參考這裏Eclipse Configuration作一些配置,不然會運行出錯。github

class ExampleActivity extends Activity { TextView title; TextView subtitle; TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); title = (TextView) findViewById(R.id.title); subtitle = (TextView) findViewById(R.id.subtitle); footer = (TextView) findViewById(R.id.footer); // TODO Use views... } }

而用ButterKnife以後的代碼是這樣的:spring

class ExampleActivity extends Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }

是否是很是簡潔易用?下面就來系統的介紹下ButterKnife的用法。ruby

Butter Knife 的特性

  1. 支持 Activity 中的 View 注入框架

  2. 支持 View 中的 View 注入eclipse

  3. 支持 View 事件回調函數注入ide

目前支持以下事件回調函數:

  • View: @OnLongClick and @OnFocusChanged.

  • TextView: @OnEditorAction.

  • AdapterView: @OnItemClick and @OnItemLongClick.

  • CompoundButton: @OnCheckedChanged.

下面來看一些注入的示例代碼:

在Activity 中注入

class ExampleActivity extends Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }

在 Fragment 中注入

public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2; @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.inject(this, view); // TODO Use "injected" views... return view; } }

在 ViewHolder 模式中注入

public class MyAdapter extends BaseAdapter { @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("John Doe"); // etc... return convertView; } static class ViewHolder { @InjectView(R.id.title) TextView name; @InjectView(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.inject(this, view); } } }

注入回調函數

下面是幾種注入回調函數的方法示例:

// 帶有 Button 參數 @OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Hello!"); } // 不帶參數 @OnClick(R.id.submit) public void submit() { // TODO submit data to server... } // 同時注入多個 View 事件 @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(); } }

Reset函數

若是須要在 界面 銷燬的時候,把注入的 View 設置爲 Null, 則能夠用 reset 函數:

public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2; @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.inject(this, view); // TODO Use "injected" views... return view; } @Override void onDestroyView() { super.onDestroyView(); Views.reset(this); } }

另外 還支持 可選的 View 注入,若是該 View 沒有,就沒有吧:

@Optional @InjectView(R.id.might_not_be_there) TextView mightNotBeThere;
 
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
  // TODO ...
}

還有兩個 findViewById 函數來簡化查找 View 的方式,若是上面都知足不了你的需求,你能夠用用他們:

View view = LayoutInflater.from(context).inflate(R.layout.thing, null); TextView firstName = Views.findById(view, R.id.first_name); TextView lastName = Views.findById(view, R.id.last_name); ImageView photo = Views.findById(view, R.id.photo);

最後,若是你是用Android Studio來做爲IDE的話,那麼有一個ButterKnife的插件android-butterknife-zelezny, 該插件能夠讓你手動生成上述注入代碼,今後讓本身成爲一個更懶惰的程序員,上張截圖吧。

相關文章
相關標籤/搜索