ButterKnife8.5.1怎么用-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“ButterKnife 8.5.1怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ButterKnife 8.5.1怎么用”這篇文章吧。

創(chuàng)新互聯(lián)專(zhuān)注于長(zhǎng)嶺企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),電子商務(wù)商城網(wǎng)站建設(shè)。長(zhǎng)嶺網(wǎng)站建設(shè)公司,為長(zhǎng)嶺等地區(qū)提供建站服務(wù)。全流程定制制作,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)

ButterKnife 簡(jiǎn)介

  ButterKnife是一個(gè)專(zhuān)注于Android系統(tǒng)的View注入框架,可以減少大量的findViewById以及setOnClickListener代碼。在7.0版本以后引入了注解處理器,取代了之前利用反射原理進(jìn)行findViewById影響APP性能的實(shí)現(xiàn)方式,不再影響APP運(yùn)行效率。

使用須知:

1.Activity中使用ButterKnife.bind(this);必須在setContentView();之后,父類(lèi)中bind后,子類(lèi)不需要再bind

2.Fragment中使用ButterKnife.bind(this, rootView); 根布局View對(duì)象,需要在onDestroyView中解綁

3.注解的屬性不能用private或static修飾,否則會(huì)報(bào)錯(cuò)

4.Activity官方例子中沒(méi)有解綁操作,而Fragment需要解綁

使用步驟:

1、添加依賴(lài)

  compile 'com.jakewharton:butterknife:8.5.1'

  annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

備注:只需要添加上述依賴(lài)即可使用。

只有當(dāng)你在Library中使用ButterKnife時(shí),才需要如下步驟:

1、在Project的build.gradle中添加:

classpath'com.jakewharton:butterknife-gradle-plugin:8.5.1'

2、在對(duì)應(yīng)Library(Module)中添加:

apply plugin:'com.jakewharton.butterknife'

網(wǎng)上許多的使用文章都把上述兩個(gè)步驟當(dāng)做是必須的。非也!

(注意:To use ButterKnife in a library)

實(shí)際項(xiàng)目

實(shí)際項(xiàng)目中一般都會(huì)有定義一個(gè)BaseActivity或BaseFragment,在基類(lèi)中綁定,則在其所有的子類(lèi)中都可以直接使用。需要注意的是,父類(lèi)中需要在子類(lèi)的setContentView調(diào)用完后再綁定。

使用完整范例

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.textview)
    TextView textview;
    @BindView(R.id.button)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_new);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.textview, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textview:
                break;
            case R.id.button:
                break;
        }
    }
}

@BindView(R.id.textview)//聲明并綁定變量
TextView textview;
//相當(dāng)于:TextViewtextView = (TextView) findViewById(R.id.textView);
 
@OnClick(R.id.button)//給按鈕綁定方法,可以不聲明引用變量
public void onViewClicked() {
}
 
@BindViews({R.id.button1, R.id.button2, R.id.button3})
public List<Button> mList ;
 
@BindString(R.string.app_name) //綁定字符串
String str;
 
@BindArray(R.array.city ) //綁定數(shù)組
String[] arr ;

在fragment中使用(需要解綁)

private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_regist_create, container, false);
    unbinder = ButterKnife.bind(this, view);//需要加多一個(gè)加載了布局的View對(duì)象
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();//fragment需要解綁
}

再偷懶一點(diǎn)

Android Studio插件:Android Butterknife Zelezny

方便給整個(gè)布局文件一鍵生成注解。

使用方法:

  在setContentView(R.layout.activity_main)中對(duì)著activity_main右鍵,generate,Generate ButterKnife Injections,勾選需要生成注解的控件即可

ButterKnife 8.5.1怎么用

圖片來(lái)自android-butterknife-zelezny

總結(jié)

一般我認(rèn)為簡(jiǎn)化了findViewById和setOnClickListener之后,已經(jīng)很方便了。并不會(huì)去使用ButterKnife中方方面面的功能,在多控件的界面例如提交表單數(shù)據(jù)時(shí)非常好用。這也就是“80/20”法則在生效吧!對(duì)于使用比較少的其他特性如解除綁定等,用到之時(shí)再?lài)L試即可!

參考文檔

[Android開(kāi)發(fā)] ButterKnife8.5.1 使用方法教程總結(jié)

同類(lèi)型框架參考

google/dagger

https://github.com/google/dagger

androidannotations

https://github.com/androidannotations/androidannotations

附錄:

注解類(lèi)型

* @BindViews

* @BindView

* @BindArray

* @BindBitmap

* @BindBool

* @BindColor

* @BindDimen

* @BindDrawable

* @BindFloat

* @BindInt

* @BindString

事件類(lèi)型

* @OnClick

* @OnCheckedChanged

* @OnEditorAction

* @OnFocusChange

* @OnItemClick item

* @OnItemLongClick

* @OnItemSelected

* @OnLongClick

* @OnPageChange

* @OnTextChanged

* @OnTouch

* @Optional

===================================================================================

Github上的使用說(shuō)明:

Download

dependencies {

 compile 'com.jakewharton:butterknife:8.5.1'

 annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

}

Snapshots of thedevelopment version are available in Sonatype's snapshots repository.

Library projects

To use ButterKnife in a library, add the plugin to your buildscript:

buildscript {

 repositories {

   mavenCentral()

  }

 dependencies {

   classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

 }

}

and then apply it in your module:

  apply plugin: 'com.android.library'

  apply plugin: 'com.jakewharton.butterknife'

Now make sureyou use R2 instead of R inside allButter Knife annotations.

classExampleActivityextendsActivity {

   @BindView(R2.id.user) EditText username;

   @BindView(R2.id.pass) EditText password;

...

}

以上是“ButterKnife 8.5.1怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站欄目:ButterKnife8.5.1怎么用-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article0/dhiooo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航網(wǎng)站改版、虛擬主機(jī)定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作