android_butterknife的简单使用

compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

layout

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.butter.MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="60dp"
        android:layout_height="60dp" />
</LinearLayout>

java

package com.example.iamchan.butter;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindBitmap;
import butterknife.BindColor;
import butterknife.BindInt;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    /*
    *
    * 绑定xml中的控件
    *
    * */
    @BindView(R.id.text)
    TextView textView;
    /*
    *
    * 绑定颜色
    *
    * */
    @BindColor(R.color.colorPrimary)
    int colorPrimary;
    /*
    *
    * 绑定字符串
    *
    * */
    @BindString(R.string.app_name)
    String name;
    /*
    * 绑定三个按钮
    *
    * */

    @BindViews({R.id.btn1,R.id.btn2,R.id.btn3})
    List<Button> buttons;
/*
* @BindArray()资源文件下的数组
* String[] arrays;
*@BindInt()
* */

/*
*
* 绑定bitmap
* */
    @BindBitmap(R.mipmap.ic_launcher)
    Bitmap ic;
/*
* 绑定img
*
* */
    @BindView(R.id.img)
    ImageView img;

/*
*
* 绑定监听事件
*
* */
@OnClick(R.id.btn1)
public void printBtn1(){
    Toast.makeText(this, "btn1", Toast.LENGTH_SHORT).show();
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        textView.setText("aaaaaaaaaaaaaaa");
        textView.setTextColor(colorPrimary);
        textView.setText(name);
        buttons.get(0).setText("btn1");
        buttons.get(1).setText("btn2");
        buttons.get(2).setText("btn3");
        img.setImageBitmap(ic);
    }
}

猜你喜欢

转载自blog.csdn.net/iamchan/article/details/82855604