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
选择Plugins,上面输入Android ButterKnife Injections後按下Install,完成後按下OK就好了
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
ButterKnife injections
confirm
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);
}
}
谢谢大家愿意花时间阅读,小弟弟我在此鞠躬
<<: [Day21]程序菜鸟自学C++资料结构演算法 – 杂凑搜寻法实作
物件宣告 物件内容为一个属性 (property)对应一个值 (value), 如果要在後方添加新的...
Amazon Relational Database Service (RDS) 是AWS中为「结构...
第一步:将昨天下载完的压缩档解压缩,取出line-bot-sdk-python-master\exa...
前言 这篇文章适合给那些要处理Legacy System(旧系统)的朋友们看,如果你们团队有系统的...
当水手也得知船长怎样 什麽是k8s TL DR 经过这三十天 阅读完的人可以得到以下技能(希望能) ...