Android Butterknife框架 注解攻略
一、原理。
最近发现一个很好用的开源框架,蛮不错的,可以简化你的代码,是关于注解的。不多说直接进入使用步骤讲解。
二、步骤。
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配置就可以了。
4、以下是图片讲解。
id="iframe_0.09802512475289404" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img0.tuicool.com/6JV3ee.png?_=3967895%22%20style=%22border:none;max-width:1513px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09802512475289404',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border: none; width: 593px; height: 710px;"> id="iframe_0.4795717536471784" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img1.tuicool.com/FBVFry.png?_=3967895%22%20style=%22border:none;max-width:1513px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4795717536471784',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border: none; width: 600px; height: 583px;"> id="iframe_0.6466978518292308" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img2.tuicool.com/6rmA32.png?_=3967895%22%20style=%22border:none;max-width:1513px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6466978518292308',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border: none; width: 600px; height: 582px;"> id="iframe_0.8217295042704791" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img0.tuicool.com/IzIvMn.png?_=3967895%22%20style=%22border:none;max-width:1513px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8217295042704791',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border: none; width: 600px; height: 579px;">
5、是用注解,直接上代码。
xml部分
<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> java部分
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!"); } }
这样就算完成了,就可以使用注解了。