初识Android Studio安装Butterknife插件

Andriod Studio 3.4.1 已经预装就一些插件,在主界面中选择菜单栏Files>Settings命令(快捷方式CTRL+ALT+S),选中其中的插件(Plugins选项).

 1.以JakeWharton的butterknife插件为例

Butterkinfe能有效的解决findViewById及各种view的监听事件,极大地简化代码,极大的提高编码效率。

 1.打开plugins

2.点击Android ButterKnife Zelezny 下面的Install 然后重启Android Studio 

3.在项目build.gradle(Modle.app)文件中加入依赖

 dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

        //导入Butterknife
        implementation 'com.jakewharton:butterknife:10.0.0'
        annotationProcessor "com.jakewharton:butterknife-compiler:10.0.0"

    }

             4.在项目布局文件中增加两个Button控件

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

   <Button
       android:id="@+id/eventone"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="事件一"/>
    <Button
        android:id="@+id/eventwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="事件二"/>
    </LinearLayout>

 5.在项目的Activity文件中选择中选择R.layout.activity_main ,点击右键,选择菜单栏中的Generate命令,弹出选择Generate Butterknife Injection 选项。

 6.选择其中的控件和点击事件,单击Condfirm按钮。

7.最后生成事件代码 

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.eventone)
    Button eventone;
    @BindView(R.id.eventwo)
    Button eventwo;

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

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


}

最后在贴下过程中所遇到的问题

Error: Static interface methods are only supported starting with Android N 

解决办法:可以通过在app的build.gradle文件中配置使用java8编译:

android {

  ...

  compileOptions {

    sourceCompatibility JavaVersion.VERSION_1_8

    targetCompatibility JavaVersion.VERSION_1_8

  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值