androidannotation 是github上的一个开源项目。
主要是注解机制,可以改善android写代码的效率。
Activity 使用
1.@EActivity 注解
可想而知,service,broadcastreceiver等都有相应的注解。
package com.joyfulmath.myannnotationsample.activity; import android.app.Activity; import com.joyfulmath.myannnotationsample.R; import com.joyfulmath.myannnotationsample.fragment.EFragmentSampleFragment; import com.joyfulmath.myannnotationsample.utils.TraceLog; import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.FragmentById; import org.androidannotations.annotations.Receiver; @EActivity(R.layout.activity_eactivity_sample) public class EActivitySampleActivity extends Activity { @FragmentById(R.id.myFragemnt) EFragmentSampleFragment eFragmentSampleFragment; @Receiver(actions = EFragmentSampleFragment.ACTION) void myReceiver() { TraceLog.i("receive:"+EFragmentSampleFragment.ACTION); } }
一个典型的Activity如上注解。R.layout.activity_eactivity_sample
这个layout表示,该activity讲加载这个layout,等同于在oncreate中setcontent一样。
@FragmentById 是fragment的注解,后面会讲到。
@Receiver使用的是 监听广播。比起老的写法,大大简化。
2.@EFragment
package com.joyfulmath.myannnotationsample.fragment; import android.content.Context; import android.content.Intent; import android.view.View; import android.widget.TextView; import com.joyfulmath.myannnotationsample.R; import com.joyfulmath.myannnotationsample.utils.TraceLog; import com.joyfulmath.myannnotationsample.view.EViewSampleView; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Click; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.RootContext; import org.androidannotations.annotations.ViewById; /** * Created by deman on 2015/11/5. */ @EFragment(R.layout.fragment_efragment_sample) public class EFragmentSampleFragment extends BaseFragment { public final static String ACTION = "efagment.samplefragment.action"; @ViewById(R.id.textFragmentView) TextView textView; @ViewById(R.id.eViewSample) EViewSampleView eViewSampleView; @Click(R.id.textFragmentView) void sendBroadCast() { eViewSampleView.setVisibility(View.VISIBLE); eViewSampleView.setSpannableText("custom define text"); doBackground(); } @Background void doBackground() { TraceLog.i("sendBroadCast"); getActivity().sendBroadcast(new Intent(ACTION)); } }
与activity类似,@EFragment 后面也是带一个layout,来表示这个fragment的布局。
里面的布局元素,使用也是非常简单:
@ViewById(R.id.textFragmentView)
TextView textView;
这句话的意思就是,在适当的时候把
textView = ((TextView) hasViews.findViewById(com.joyfulmath.myannnotationsample.R.id.textFragmentView));
在appuildgeneratedsourceaptdebugcomjoyfulmathmyannnotationsamplefragment
下面会生成类+“_”.
这个类才是实际真正运行的类。
当然要运用fragment,和老的写法是一致的:
<?xml version="1.0" encoding="utf-8"?> <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" 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.joyfulmath.myannnotationsample.activity.EActivitySampleActivity"> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/myFragemnt" class="com.joyfulmath.myannnotationsample.fragment.EFragmentSampleFragment_" /> </RelativeLayout>
3.Custom Class
这个写法更加简单:@EBean
用这个修饰词,表示class的构造只有默认,以及带context参数的2种结构。
在其他地方使用这个calss的时候,使用@Bean注解。
@EBean public class NotificationMgr { @SystemService NotificationManager notificationManager; @RootContext Context context;
看到@RootContext 是构造函数用的context。
看使用的地方,debug里面可以看到:
notificationMgr = NotificationMgr_.getInstance_(this);//this 就是当前activity
4.custom view
自定义view的关键是2点:
@EView关键字
构造函数如下:
@EView public class EViewSampleView extends TextView { public EViewSampleView(Context context,AttributeSet attributes) { super(context,attributes); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } public void setSpannableText(CharSequence text) { TraceLog.i(text.toString()); SpannableString spannableString = new SpannableString(text); spannableString.setSpan(new ForegroundColorSpan(Color.GREEN),1,spannableString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); setText(spannableString); } }
其他就和自定义view一样了。
参考:
https://github.com/excilys/androidannotations/wiki/Cookbook