总结性发言:这是一款节省代码的框架。
原理:使用大量的标签来代替重复代码。
详细资料文档:https://github.com/androidannotations/androidannotations/wiki
使用方法:下载jar包,导入自己工程,我这里是androidannotations-api-3.2.jar
下面是我的测试代码:
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById(R.id.btn_1)
Button btn;
@ViewById
TextView textView1;
@ViewsById({R.id.textView2,R.id.textView3})
List<TextView> textViewList;
public static final String NAME_KEY="name_key";
public static final String AGE_KEY="age_key";
@Click(R.id.btn_1)
public void startActivity(){
doSomething();
// Intent intent = new Intent(MainActivity.this,SecondActivity_.class);
// intent.putExtra(NAME_KEY, "Nail");
// intent.putExtra(AGE_KEY, "18");
// startActivity(intent);
// Intent service = new Intent(MainActivity.this,MyService_.class);
// startService(service);
}
@AfterViews
public void setTextView(){
textView1.setText("你好");
int i=0;
for(TextView tv:textViewList){
tv.setText("我是"+i++);
}
}
@Background
public void doSomething(){
Log.i("nail", "Background Thread id:"+Thread.currentThread().getId());
updateUI();
System.out.println("sds");
}
@UiThread
public void updateUI(){
textView1.setText("Changed");
Log.i("nail", "UI Thread id:"+Thread.currentThread().getId());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("nail", "Main Thread id:"+Thread.currentThread().getId());
// setContentView(R.layout.activity_main);
}
}
MyService
@EService
public class MyService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("nate", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
SecondActivity
@EActivity(R.layout.activity_second)
public class SecondActivity extends Activity{
@Extra(MainActivity.NAME_KEY)
String name;
@Extra(MainActivity.AGE_KEY)
String age;
@ViewById
TextView tv1;
@ViewById
TextView tv2;
@AfterViews
public void initView() {
tv1.setText(name);
tv2.setText(age);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
说明:第一个MainActivity中 onCreate 里注释的部分解除掉就可以测试 跳转,启动服务的demo,下面两个就是测试所需的代码,非常简单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity_"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity_" />
<service android:name=".MyService_"/>
</application>
activity_main.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidannotation.MainActivity"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View_2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View_3" />
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="41dp"
android:layout_marginTop="94dp"
android:text="Annotation" />
</LinearLayout>
activity_second.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidannotation.MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecondActivity1" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecondActivity2" />
</LinearLayout>