溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android Studio中ButterKnife插件的安裝與使用詳解

發(fā)布時(shí)間:2020-10-20 15:23:14 來(lái)源:腳本之家 閱讀:214 作者:cxc19890214 欄目:編程語(yǔ)言

1》Android Studio 安裝ButterKnife插件

同安裝其他插件類(lèi)似,如下:

1.1》打開(kāi)Plugins界面

Android Studio中ButterKnife插件的安裝與使用詳解

Android Studio中ButterKnife插件的安裝與使用詳解

按照上圖中1,2,3指示操作(注意:這里我的Android Studio中已經(jīng)安裝了該插件,所以顯示的內(nèi)容不太一樣)。然后重啟Android Studio。

2》在項(xiàng)目上使用該開(kāi)源項(xiàng)目(以Android Studio 為例)

2.1》在bulid.gradle中添加依賴(lài)

Android Studio中ButterKnife插件的安裝與使用詳解

重新編譯一下該項(xiàng)目,通過(guò)后繼續(xù)操作。

2.2》在代碼中就可以使用注解的方式了

2.2.1》示例布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context=".MainActivity"> 
 
  <TextView 
    android:id="@+id/text_veiw_tv1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView 1" /> 
 
  <Button 
    android:id="@+id/button_bt1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Button1" /> 
 
  <TextView 
 
    android:id="@+id/text_veiw_tv2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView 2" /> 
 
 
  <Button 
    android:id="@+id/button_bt2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Button2" /> 
 
 
</LinearLayout> 

2.2.2》在代碼中使用注解

選擇上述布局文件名,右鍵

Android Studio中ButterKnife插件的安裝與使用詳解

Android Studio中ButterKnife插件的安裝與使用詳解

Android Studio中ButterKnife插件的安裝與使用詳解

選擇“Confirm”后,就會(huì)自動(dòng)生成各個(gè)在布局文件中帶有id 屬性的view的注解形式

如下所示:

@Bind(R.id.text_veiw_tv1) 
TextView textVeiwTv1; 
@Bind(R.id.text_veiw_tv2) 
TextView textVeiwTv2; 
@Bind(R.id.button_bt1) 
Button buttonBt1; 
@Bind(R.id.button_bt2) 
Button buttonBt2; 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  ButterKnife.bind(this); 
 
 
} 

標(biāo)注如下:

Android Studio中ButterKnife插件的安裝與使用詳解

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI