Eclipse使用Butterknife的相關配置

eclipse使用Butterknife的相關配置


Butterknife是Android一個註解的開源框架,使用簡單,可以幫助我們初始化view、設置view監聽等操作,省去了findViewById()提高開發的效率。

官網:http://jakewharton.github.io/butterknife/

jar包下載:http://repo1.maven.org/maven2/com/jakewharton/butterknife/7.0.1/butterknife-7.0.1.jar


下面講解一下在eclipse中如何使用butterknife:

步驟一:下載jar包,放到工程libs目錄下,完成了這一步還不行,還要進行步驟二

步驟二:

(2.1)選擇你的項目右鍵---->properties----->java compiler------>選中Annotation Processing------->勾選enable project specific settings

(2.2)展開Annotation Processing------>選中Factory Path ---->勾選enable project specific settings------>add JARs------>選中你的butterknife.jar

注意:如果步驟二沒有Annotation Processing選項,可參照這篇文章解決:http://blog.csdn.net/lpforever/article/details/40779341


做完以上配置後就可以使用Butterknife了,但是要注意:屬性佈局不能用private or static 修飾,否則會報錯


(1)在activity中使用


public class MainActivity extends Activity
{
    @Bind(R.id.tv_butt) TextView tvb;
    @Bind(R.id.tv_gutt) TextView tvg;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);//在這裏初始化

     }

   @Override
    protected void onDestroy()
    {
        super.onDestroy();
        ButterKnife.unbind(this);//不要忘記在Activity或者Fragment銷燬的時候調用
    }
}


(2)在Fragment中使用

public class MyFragment extends Fragment {   


    protected Context context; 
  
    @Nullable  
    @Override  
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        View view =inflater.inflate(R.layout.my_layout,container,false);  
        ButterKnife.bind(this,view);//綁定framgent  
        return view;  
    }   
  
    @Override  
    public void onDestroyView() {  
        super.onDestroyView();  
        ButterKnife.unbind(this);//解綁  
    }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章