1,在gradle中添加:
依赖中添加:
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
2,在xml中添加一个按钮id为button'
public class BlankFragment extends Fragment {
private Unbinder unbinder;
@BindView(R.id.button)
Button button;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank, container, false);
unbinder = ButterKnife.bind(this,view);
return view;
}
@OnClick(R.id.button)
void showtoast(){
Toast.makeText(getActivity(),"123",Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroyView() {
if (unbinder != null) {
unbinder.unbind();
}
super.onDestroyView();
}
}
3,有一次报错:
java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
需要在gradle中添加:
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}