标签:android blog http io ar color os 使用 sp
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/41577217
androidannotation是一个非常牛逼的框架(https://github.com/excilys/androidannotations/wiki),可以做到:依赖注入(Dependency Injection),简化的线程模型(Simplified threading model),事件绑定(Event binding),REST Client。
非常好用,更重要的是它对性能无影响!本文我们简要的来看一下一些入门的东西。<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="com.example.hello.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="com.example.hello.SecondActivity_" />
</application>
</manifest>里面定义了两个activity,注意名字后面都带了一个下划线。@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById(R.id.myInput)
EditText myInput;
@ViewById(R.id.myTextView)
TextView textView;
@ViewById(R.id.myButton2)
Button btn2;
@StringRes(R.string.go_to_second)
String btn2Txt;
@AfterViews
protected void afterViews(){
btn2.setText(btn2Txt);
}
@Click
void myButton() {
String name = myInput.getText().toString();
textView.setText("Hello " + name);
}
@Click
void myButton2(){
SecondActivity_.intent(this).start();
}
}
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:orientation="vertical" >
<EditText
android:id="@+id/myInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me!" />
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/myButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_second"/>
</LinearLayout>SecondActivity的源码就不贴了。<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
<compilerArgs>
<arg>-Atrace=true</arg>
<arg>-AlogLevel=trace</arg>
<arg>-AlogConsoleAppender=true</arg>
<arg>-AandroidManifestFile=/path/to/AndroidManifest.xml</arg>
</compilerArgs>
</configuration>
</plugin>所有的可用的参数如下:public final class MainActivity_ extends MainActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
init_(savedInstanceState);//这里会处理@StringRes,@ColorRes等等
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);//这里对应@EActivity(R.layout.activity_main)
}
private void init_(Bundle savedInstanceState) {
Resources resources_ = this.getResources();
btn2Txt = resources_.getString(string.go_to_second);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
afterSetContentView_();
}
@Override
public void setContentView(View view, LayoutParams params) {
super.setContentView(view, params);
afterSetContentView_();
}
@Override
public void setContentView(View view) {
super.setContentView(view);
afterSetContentView_();
}
private void afterSetContentView_() {
//@ViewById
textView = ((TextView) findViewById(id.myTextView));
myInput = ((EditText) findViewById(id.myInput));
btn2 = ((Button) findViewById(id.myButton2));
//@Click
{
View view = findViewById(id.myButton2);
if (view!= null) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
MainActivity_.this.myButton2();
}
}
);
}
}
//@Click
{
View view = findViewById(id.myButton);
if (view!= null) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
MainActivity_.this.myButton();
}
}
);
}
}
afterViews();//这里调用了@AfterViews标注的afterViews()方法
}
public static MainActivity_.IntentBuilder_ intent(Context context) {
return new MainActivity_.IntentBuilder_(context);
}
public static class IntentBuilder_ {//这个是给startActivity和startActivityForResult用的
private Context context_;
private final Intent intent_;
public IntentBuilder_(Context context) {
context_ = context;
intent_ = new Intent(context, MainActivity_.class);
}
public Intent get() {
return intent_;
}
public MainActivity_.IntentBuilder_ flags(int flags) {
intent_.setFlags(flags);
return this;
}
public void start() {
context_.startActivity(intent_);
}
public void startForResult(int requestCode) {
if (context_ instanceof Activity) {
((Activity) context_).startActivityForResult(intent_, requestCode);
} else {
context_.startActivity(intent_);
}
}
}
}最后看一下完整的pom.xml的例子(https://github.com/excilys/androidannotations/blob/develop/examples/maveneclipse/pom.xml):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.androidannotations</groupId>
<artifactId>maveneclipse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
<name>maveneclipse</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<android.version>4.1.1.4</android.version>
<android.platform>16</android.platform>
<androidannotations.version>3.2</androidannotations.version>
<java.version>1.6</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<version>${androidannotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.androidannotations</groupId>
<artifactId>androidannotations-api</artifactId>
<version>${androidannotations.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgs>
<arg>-AlogLevel=trace</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.3</version>
<configuration>
<sdk>
<platform>${android.platform}</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
标签:android blog http io ar color os 使用 sp
原文地址:http://blog.csdn.net/goldenfish1919/article/details/41577217