[Lesson20] ButterKnife

ButterKnife可以让我们在宣告元件时之後不用再打findViewById这行,可以帮忙省下宣告元件的时间。

build.gradle:

dependencies {
  implementation 'com.jakewharton:butterknife:10.2.3'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
}
android {
  ...
  // Butterknife requires Java 8.
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

ButterKnife Injections:

Android Studio中选择 File -> Settings
https://ithelp.ithome.com.tw/upload/images/20210924/20129566gPBDdsg3g4.png

选择Plugins,上面输入Android ButterKnife Injections後按下Install,完成後按下OK就好了
https://ithelp.ithome.com.tw/upload/images/20210924/20129566I2aJDaTp7u.png

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="HELLO"/>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_below="@+id/text"/>
</RelativeLayout>

MainActivity:
右键 -> Generate
https://ithelp.ithome.com.tw/upload/images/20210924/20129566M6aToszvAM.png

ButterKnife injections
https://ithelp.ithome.com.tw/upload/images/20210924/20129566HteMwHd2LA.png

confirm
https://ithelp.ithome.com.tw/upload/images/20210924/20129566j4bxzAjBER.png

public class MainActivity extends AppCompatActivity {

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

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

谢谢大家愿意花时间阅读,小弟弟我在此鞠躬/images/emoticon/emoticon41.gif


<<:  [Day21]程序菜鸟自学C++资料结构演算法 – 杂凑搜寻法实作

>>:  Angular 笔记 CSS 样式篇

【Day17】物件结构与存取

物件宣告 物件内容为一个属性 (property)对应一个值 (value), 如果要在後方添加新的...

DAY 9 Big Data 5Vs – Velocity(多样性) RDS

Amazon Relational Database Service (RDS) 是AWS中为「结构...

[DAY8]将范例上传(2)

第一步:将昨天下载完的压缩档解压缩,取出line-bot-sdk-python-master\exa...

26. 如何淘汰万年遗毒的code

前言 这篇文章适合给那些要处理Legacy System(旧系统)的朋友们看,如果你们团队有系统的...

Day1-当水手也得知船长怎样 什麽是k8s

当水手也得知船长怎样 什麽是k8s TL DR 经过这三十天 阅读完的人可以得到以下技能(希望能) ...