7、ButterKnife注解在eclipse中應(yīng)用

jar包下載:

http://download.csdn.net/detail/seven2729/9147765

點(diǎn)擊事件沒(méi)反應(yīng):
右鍵項(xiàng)目配置properties>>java compiler>>Annotation processing>>Factory Path,
勾選Enable project specific settings ,然后Add JARS,從項(xiàng)目libs文件夾下選擇依賴包,點(diǎn)擊完成。
  1. 在Activity里的寫(xiě)法

    // 綁定View對(duì)象
     @Bind(R.id.title) TextView title;
     @Bind(R.id.subtitle) TextView subtitle;
     @Bind(R.id.footer) TextView footer;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         ButterKnife.bind(this);
         // TODO 在這里直接用變量即可
     }
    
  2. 在Fragment里的寫(xiě)法:
    注意最好在onDestoryView中進(jìn)行解綁

/**
* Fragment生命周期比較特殊
* 需要在onDestroyView及時(shí)釋放這些View引用,避免造成內(nèi)存溢出
*/
public class FancyFragment extends Fragment {
    @Bind(R.id.button1)
    Button button1;
    @Bind(R.id.button2)
    Button button2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fancy_fragment, container, false);
        ButterKnife.bind(this, view); // 進(jìn)行綁定
        // TODO Use fields...
        return view;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this); // 解除綁定
    }
}
  1. 在數(shù)據(jù)適配器的ViewHolder中的寫(xiě)法:
    @Bind(R.id.title) TextView name;
    @Bind(R.id.job_title) TextView jobTitle;
    public ViewHolder(View view) {
      ButterKnife.bind(this, view); // 進(jìn)行綁定, 將view中的指定id對(duì)當(dāng)前對(duì)象進(jìn)行注入
    }
  1. 依賴注入的其他寫(xiě)法:
// 也可以將這些View放到集合中
    @Bind({ R.id.title, R.id.subtitle, R.id.footer })
    List<TextView> textViews;
    // 綁定字符串
    @BindString(R.string.app_name) String titile;
    // 綁定圖片
    @BindDrawable(R.drawable.ic_launcher) Drawable drawable;
    // 綁定顏色值, 也可以是顏色選擇器
    @BindColor(R.color.colorPrimary) int colorPrimary; // int or ColorStateList field
    // 綁定以像素為單位的尺寸或數(shù)值
    @BindDimen(R.dimen.spacer) float spacer; // int (for pixel size) or float (for exact value) field
    /**
     * 給指定控件添加點(diǎn)擊事件, 注意:參數(shù)View可以省略, 或者寫(xiě)成Button
     * @param view
     */
    @OnClick(R.id.bt_test)
    public void submit(View view) {
        // TODO submit data to server...
        Toast.makeText(this, "single declare!", Toast.LENGTH_SHORT).show();
    }
    /**
     * 給多個(gè)控件添加點(diǎn)擊事件, 注意:參數(shù)View可以省略, 或者寫(xiě)成Button
     * @param view
     */
    @OnClick({R.id.bt_test, R.id.bt_test2})
    public void submit2(View view) {
        // TODO submit data to server...
        switch (view.getId()) {
            case R.id.bt_test:
                Toast.makeText(this, "multi declare1!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_test2:
                Toast.makeText(this, "multi declare2!", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    /**
     * 添加長(zhǎng)按事件
     * @return 表示是否消費(fèi)長(zhǎng)按事件  true消費(fèi)
     */
    @OnLongClick(R.id.bt_test) boolean sayGetOffMe() {
        Toast.makeText(this, "Let go of me!", Toast.LENGTH_SHORT).show();
        return true;
    }
    // 給ListView添加條目點(diǎn)擊監(jiān)聽(tīng)
    @OnItemSelected(R.id.list_view)
    void onItemSelected(int position) {
        // TODO ...
    }

5、添加混淆忽略:

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容