ButterKnife注入注解框架用法

Android 依赖注入 ButterKnife 基本使用 - 渐行渐远渐无声 - 博客园
http://www.cnblogs.com/fansen/p/5653887.html

ButterKnife使用详解 - ITjianghuxiaoxiong的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549

JakeWharton/butterknife: Bind Android views and callbacks to fields and methods.
https://github.com/JakeWharton/butterknife

avast/android-butterknife-zelezny: Android Studio plug-in for generating ButterKnife injections from selected layout XML.
https://github.com/avast/android-butterknife-zelezny

Sample Code:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.edit_name)
    EditText userName;

    @BindView(R.id.edit_pass)
    EditText password;

    @BindView(R.id.tv_hint)
    TextView hintTxt;

    @BindView(R.id.btn_confirm)
    Button confirmBtn;

    @BindView(R.id.checkbox)
    CheckBox checkBox;


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

    }

    @OnClick(R.id.btn_confirm)
    public void onClick() {  // or submit() ...
        Toast.makeText(this, "信息提交中!", Toast.LENGTH_SHORT).show();
        if ( TextUtils.isEmpty(userName.getText()) || TextUtils.isEmpty(password.getText()) ) {
            hintTxt.setText("用户名或密码为空");
            hintTxt.setTextColor(Color.RED);
            hintTxt.setVisibility(View.VISIBLE);
        }
    }

//    onCheckedChanged impl method 1
    @OnCheckedChanged(R.id.checkbox)
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            //选择状态 显示明文--设置为可见的密码
            password.setTransformationMethod(
                    HideReturnsTransformationMethod.getInstance() );
        } else {
            //默认状态显示密码为不可见的黑点
            password.setTransformationMethod(
                    PasswordTransformationMethod.getInstance() );
        }
    }

    //    onCheckedChanged impl method 2
    public void onCheckedChanged2(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            //选择状态 显示明文--设置为可见的密码
            password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            //默认状态显示密码为不可见的黑点
            password.setInputType(InputType.TYPE_CLASS_TEXT |
                    InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }

}

Zelezny插件的使用

在AndroidStudio->File->Settings->Plugins->搜索Zelezny下载添加就行 ,可以快速生成对应组件的实例对象,不用手动写。使用时,在要导入注解的Activity 或 Fragment 或 ViewHolder的layout资源代码上,右键——>Generate——Generate ButterKnife Injections,然后就出现如图的选择框。(此动态图来自官网)

原文地址:https://www.cnblogs.com/bluestorm/p/6650730.html