代码语言:javascript复制
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(...);
// ...
代码语言:javascript复制you can simply do it in this way
public class MainActivity extends AppCompatActivity {
@BindView(R.id.fab) FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fab.setOnClickListener(...);
// ...
there are more features in this plugin. you can view it in this page https://github.com/JakeWharton/butterknife
接下来楼主懒得用英文了感觉还是中文好读
安装时可能遇到的问题
Error:(3, 0) Android Gradle plugin 3.0.0 must not be applied to project since version 3.0.0 was already applied to this project
代码语言:javascript复制官方文档让我们在module的gradle顶部加这两句
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
代码语言:javascript复制我通过删掉下面这句,解决了问题
apply plugin: 'com.android.library'
Gradle sync failed: Cause: com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;
代码语言:javascript复制写文章时, 最新版本是8.8.1, 我通过改成使用8.4.0版本解决了问题。 在项目的gradle中改为如下
buildscript {
repositories {
// ...
}
dependencies {
// ...
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}
}