原文:http://www.tuicool.com/articles/2e2qMf2
一、原理。
最近發現一個很好用的開源框架,蠻不錯的,可以簡化你的代碼,是關於註解的。不多說直接進入使用步驟講解。
二、步驟。
1、準備階段,先到官網( http://jakewharton.github.io/butterknife/ )上jar包,下載下來。
2、把下載下來的jar包,放到項目的libs下,就會自動導入項目了。
3、配置eclips,鼠標對準需要註解的項目,單擊右鍵 poperties –>java Compiler –>
Annotation Procession –> 鉤一下 Enable project specific settings 其它的就會自動鉤上了
–> Factory Path ( 鉤一下Enable project specific settings )–> 最後Add …. JARs 把剛剛下載的jar包來。這樣eclips配置就可以了。
三、ButterKnife的使用
Xml部分
<span style="font-size:18px;"><RelativeLayout 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" >
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout></span>
java部分
<span style="font-size:18px;">package com.msquirrel.main;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@InjectView(R.id.tv_test)
TextView tvTest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
tvTest.setText("test");
}
@OnClick(R.id.tv_test)
public void sayHi() {
tvTest.setText("Hello!");
}
}</span>