Android androidannotations

 

androidannotations是一款Android注入框架,可以方便我们编程,减少代码量(变相减少了错误的可能),让我们可以更多的把精力放在逻辑处理上。

本文API介绍取至https://github.com/excilys/androidannotations/wiki/Cookbook

配置

在AS中,需要在项目的build.gradle中进行加入如下配置(有注释部分)

buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
 		classpath 'com.android.tools.build:gradle:1.0.0'
 		// 使用android-apt
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
	}
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
	def AAVersion = '3.2'
	// 设置版本
	apt "org.androidannotations:androidannotations:$AAVersion" 
	compile "org.androidannotations:androidannotations-api:$AAVersion"
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.myproject.name'	// 指定包名
	}
}

使用

  • 基本使用示例
@EActivity(R.layout.translate)	// 设置布局文件
public class TranslateActivity extends Activity {
	@ViewById	// 注入 等同于 findViewById(R.id.textInput)
	EditText textInput;

	@ViewById(R.id.myTextView)
	TextView result;

	@AnimationRes	// 获取 anroid.R.anim.fade_in
	Animation fadeIn;

	@Click	// 设置R.id.doTranslate的监听
	void doTranslate() {
		translateInBackground(textInput.getText().toString());
    }

    @Background // 后台线程
    void translateInBackground(String textToTranslate) {
        String translatedText = callGoogleTranslate(textToTranslate);
        showResult(translatedText);
    }

    @UiThread // UI线程
    void showResult(String translatedText) {
        result.setText(translatedText);
        result.startAnimation(fadeIn);
    }

}

Activity

  • @EActivity注入Activity
@EActivity(R.layout.main)
public class MyActivity extends Activity {
	
}

也可以不注入

@EActivity
public class MyListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
	}
}

注:使用Androidannotations注入的Activity在AndroidManifest.xml中配置时,名字后需要加上_,如MainActivity则为.MainActivity_

 

Fragment

  • @EFragment
@EFragment
public class MyFragment extends Fragment {
	
}

在布局中需要使用MyFragment_表示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/myFragment"
        android:name="com.company.MyFragment_"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

在程序中使用

MyFragment fragment = new MyFragment_();

也可以注入布局

@EFragment(R.layout.my_custom_layout)
public class MyFragment extends ListFragment {
  // R.layout.my_custom_layout will be injected
}

获取Fragment时

@EActivity(R.layout.fragments)
public class MyFragmentActivity extends FragmentActivity {
  @FragmentById
  MyFragment myFragment;

  @FragmentById(R.id.myFragment)
  MyFragment myFragment2;

  @FragmentByTag
  MyFragment myFragmentTag;

  @FragmentByTag("myFragmentTag")
  MyFragment myFragmentTag2;
}

自定义控件

@EView
public class CustomButton extends Button {
  @App
  MyApplication application;

  @StringRes
  public CustomButton(Context context,AttributeSet attrs) {
    super.(context,attrs);
  }
}

在布局中使用时

<!-- ... -->
<com.myapp.view.CustomButton_
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

自定义ViewGroups

layout

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/image"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="http://seniorzhai.github.io/2015/02/28/androidannotations%E7%AE%80%E4%BB%8B/@drawable/check" />

    <TextView
        android:id="@+id/title"
        android:layout_toLeftOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="12pt" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:textColor="#FFdedede"
        android:textSize="10pt" />

</merge>

Java代码

@EViewGroup(R.layout.title_with_subtitle)
public class TitleWithSubtitle extends RelativeLayout {
  @ViewById
  protected TextView title,subtitle;

  public TitleWithSubtitle(Context context,AttributeSet attrs) {
    super(context,attrs);
  }

  public void setTexts(String titleText,String subTitleText) {
    title.setText(titleText);
    subtitle.setText(subTitleText);
  }
}

使用

<com.myapp.viewgroup.TitleWithSubtitle_
  android:id="@+id/firstTitle"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

Service

@EService
public class MyService extends IntentService {
  @SystemService
  NotificationManager notificationManager;
  
  @Bean 
  MyEnhancedDatastore datastore;

  @RestService
  MyRestClient = myRestClient;

  @OrmLiteDao(helper = DatabaseHelper.class,model = User.class)
  UserDao userDao;

  public MyService() {
    super(MyService.class.getSimpleName);
  }

  @Override
  proteced void onHandleIntent(Intent intent){
    showTast();
  }

  @UiThread
  void showToast() {
    Toast.makeText(getApplicationContext(),"Hi~",Toast.LENGTH_LONG).show();
  }
}

对于IntentService还可以设置一些操作

@ServiceAction
void mySimpleAction(String param) {
  //...
}

通过MyIntentService_.intent(getApplication()).myAction("test").start()使用

获取资源

// String
@StringRes(R.string.hello)
String myHelloString;
@StringRes
String hello;
// Color
@ColorRes(R.color.backgroundColor)
int someColor;
@ColorRes
int backgoundColor;
// animation
@AnimationRes(R.anim.fadein)
Animation fadein;
@AnimationRes
Animation fadein;
// DimensionRes
@DimensionRes(R.dimen.fontsize)
float fontSizeDimension;
@DimensionRes
float fontsize;
// 换算成PX
@DimensionPixelOffsetRes(R.sting.fontsize)
int fontSizeDimension;
@DimensionPixelOffsetRes
int fontsize;

其他

  • @BooleanRes
  • @ColorStateListRes
  • @DrawableRes
  • @IntArrayRes
  • @IntegerRes
  • @LayoutRes
  • @MovieRes
  • @TextRes
  • @TextArrayRes
  • @StringArrayRes

androidannotations是一款Android注入框架,可以方便我们编程,减少代码量(变相减少了错误的可能),让我们可以更多的把精力放在逻辑处理上。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值