Android-butterknife simple and practical
Butterknife is a dependency injection framework, which saves us the findviewbyid () operation. We have been using the injection function in xutils all the time. This time, we have experienced this and I feel that there are still some differences.
Instructions for use:
@ Viewinject (r. id. xxx)
Textview xxx;
The textview cannot be private or static. Otherwise, an error is returned. This restriction is not found in xutils.
@ OnLongClick (R. id. xxx)
Boolean method ()
The method here cannot be private, and the return type must be boolean.
Test code:
1. Add dependency:
compile 'com.jakewharton:butterknife:6.1.0'
2. layout code:
3. activity Code:
public class MainActivity extends ActionBarActivity { @InjectView(R.id.tv_hello) TextView tv_hello; @InjectView(R.id.bt_changetext) Button bt_changetext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this);// bt_changetext.setOnClickListener(new View.OnClickListener() {// @Override// public void onClick(View v) {//// }// }); }// @OnClick(R.id.bt_changetext)// void changeText() {// tv_hello.setText("hello my dear graypn!");// } @OnLongClick(R.id.bt_changetext) boolean changeTextByLongClick() { tv_hello.setText("hello my dear graypn!"); return true; }}