概述
今天来看看Android的support包中为我们提供的一套注解,
总共可分为以下几类注解
1.Null注解
2.资源类型注解
3.类型定义注解
4.线程注解
5.值范围注解
6.权限注解
7.重写函数注解
8.返回值注解
9.Keep注解
Null注解和资源类型注解
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testNull(null);
testBoolean(0);
testColor(getColor(R.color.colorAccent));
testDrawable(R.drawable.ic_launcher);
testLayout(R.layout.activity_main);
findId(R.id.bottom);
testString(R.string.app_name);
testStyle(R.style.AppTheme);
}
//参数不能为空,为null会给出警告
public void testNull(@NonNull String content){
}
//返回值可以为空
public @Nullable String getStr(){
return null;
}
//标记整型值是布尔类型
public void testBoolean(@BoolRes int flag){
}
//标记整型值是R.color类型
public void testColor(@ColorRes int color) {
}
//标记参数类型为getResource().getColor类型
public void testColor2(@ColorInt int color) {
}
//标记类型是R.drawable
public void testDrawable(@DrawableRes int resId){
}
//只能设置R.layout的参数
public void testLayout(@LayoutRes int layoutResId) {
}
//参数类型是R.id类型
public void findId(@IdRes int id){
}
//参数类型是R.string
public void testString(@StringRes int content){
}
//参数类型是R.style
public void testStyle(@StyleRes int styleRedId){
}
类型定义注解
public class ActionType {
private static final int DEFAULT_TYPE = ActionType.ONE;
private @Type int type;
@Retention(RetentionPolicy.SOURCE)
//定义可以接收的常量列表
@IntDef({ONE, TWO, THREE})
@interface Type {
}
static final int ONE = 1;
static final int TWO = 2;
static final int THREE = 3;
@Type
public int getType(){
return DEFAULT_TYPE;
}
public void setType(@Type int type){
this.type = type;
}
//================测试带标记的
@Retention(RetentionPolicy.RUNTIME)
//flag标记代表是否可以把常量作为一个标记,也就是是否可以设置多个,默认false
@IntDef(flag = true,value = {ONE, TWO, THREE})
@interface TYPE2 {
}
@TYPE2
public int getType2(){
return DEFAULT_TYPE;
}
public void setType2(@TYPE2 int type){
}
使用如下
//测试@intDef
ActionType actionType = new ActionType();
//设置类型
// actionType.setType(0);有警告,必须为指定的类型
actionType.setType(ActionType.ONE);
//获取类型
actionType.getType();//提示你获取返回值
//带标记的,可以设置多个
actionType.setType2(ActionType.ONE|ActionType.TWO);
线程注解
@Override
protected void onStart() {
super.onStart();
SimpleTask simpleTask = new SimpleTask();
simpleTask.execute();
}
static class SimpleTask extends AsyncTask<URL,Integer,Long>{
@Override
@MainThread//通常注解生命周期相关的函数
protected void onPreExecute() {
super.onPreExecute();
}
@Override
@WorkerThread//后台线程
protected Long doInBackground(URL[] objects) {
return null;
}
@Override
@MainThread
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
@MainThread
protected void onPostExecute(Long o) {
super.onPostExecute(o);
}
}
@UiThread通常用来注解和View相关的,比如View就用了
@UiThread
public class View implements Drawable.Callback, KeyEvent.Callback,
AccessibilityEventSource {
@BinderThread标记用在Binder线程中
值范围注解
通常用于数组,集合,字符串
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setAlpha(260);值超出范围会有红色警告
setAlpha(255);
setText("aaaaaa");
setText2("222");
}
//传入的参数只能在0-255
public void setAlpha(@IntRange(from = 0,to = 255) int alpha){
}
//传入的字符串长度在4-6
public void setText(@Size(min = 4,max = 6) String text){
}
//传入的字符串长度必须是3
public void setText2(@Size(3) String text){
}
//传入的字符串长度必须是2的倍数
public void setText3(@Size(multiple = 2) String text){
}
权限注解
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callPhone();//如果没有添加权限,会提示对应的权限
store();
}
@RequiresPermission(Manifest.permission.CALL_PHONE)
public void callPhone(){
}
//需要所有的权限
@RequiresPermission(allOf = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE})
public void store(){
}
//需要任意一个权限
@RequiresPermission(anyOf = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE})
public void store2(){
}
返回值注解
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkResule();
}
//注解的函数如果返回值没有接收,会有黄色警告
@CheckResult
public String checkResule(){
return "";
}
}
Keep注解
用来标记在Proguard过程中不需要混淆的地方