在代码里直接使用注解,然后就可以省好多事情啊~~~~
@AfterViews
@AfterViews
void afterViews() {
// TODO: Do something after the views are injected
myTextView.setText("Hello World!");
}
The @AfterViews annotation indicates that a method should be called after the views binding has happened.
@AfterViews 注解标识在视图绑定操作发生后,有个方法需要被调用,一般可以使用init方法名。
When onCreate() is called, @ViewById fields are not set yet. Therefore, you can use@AfterViews on methods to write code that depends on views.
当 onCreate() 方法调用后, @ViewById 字段还没有设置好。因此,你可以用@AfterViews 注解绑定的方法添加处理视图相关的代码。
你可以用 @AfterViews注解多个方法。不要忘记在 onCreate()方法中不能使用任何视图字段。
@<strong>EActivity</strong>(R.layout.main)
public class MyActivity extends Activity {
@ViewById
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DON'T DO THIS !! It will throw a NullPointerException, myTextView is not set yet.
// myTextView.setText("Date: " + new Date());
}
Recall that injection is always made as soon as possible. Therefore, it's safe to use any field annotated, e.g., with @Extra or @InstanceState in @AfterViews methods as these tags don't require any view to be set (as @AfterViews do). Therefore you can safely assume that such fields will be already initialized with their intended values in methods annotated with @AfterViews:
回想一下,注入总是以最快的速度完成。因此,在@AfterViews方法中使用任何不需要设置视图(像@AfterViews要做的)注解字段,比如@Extra 或者 @InstanceState,都是安全的。所以,你可以放心地假设这些字段在加了@AfterViews注解的方法中初始化值是安全的。
@EActivity(R.layout.main)
public class MyActivity extends Activity {
@ViewById
TextView myTextView;
@InstanceState
Integer textPosition;
@AfterViews
void updateTextPosition() {
myTextView.setSelection(textPosition);
//Sets the cursor position of myTextView
}
@EActivity(R.layout.main)
public class MyActivity extends Activity {
@ViewById
TextView myTextView;
@InstanceState
Integer textPosition;
@AfterViews
void updateTextPosition() {
myTextView.setSelection(textPosition);
//Sets the cursor position of myTextView
}
@Ebean,@RootContext,@Background,@UiThread,@AfterInject
package com.example.androidannotations;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
import com.googlecode.androidannotations.annotations.AfterInject;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
@EBean
public class Student {
//RootContext能取到调用该Bean的Context,构造方法不再需要传Context参数
@RootContext
Context context;
@RootContext
MainActivity activity;
//ViewById也能在这里直接使用
@ViewById
TextView tv;
public void Toast()
{
Toast.makeText(context, "在Ebean中调用", Toast.LENGTH_LONG).show();
}
//后台线程执行
@Background
public void backThread()
{
for(int i=0;i<9999;i++)
{
try {
Thread.sleep(1000);
// activity.updateTv(i);
//更新UI,调用在UI线程执行的方法
updateTv(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//UiThread在UI线程执行
@UiThread
public void updateTv(int i)
{
tv.setText(String.valueOf(i));
}
//AfterInject在构造方法执行完成后执行
@AfterInject
public void doSomethingAfterInject()
{
System.out.println("Student AfterInject");
}
}
Activity中,资源的导入,都不需要手动写代码了,查找id也不需要手写了
The @ViewById annotation indicates that an activity field should be bound with the corresponding View component from the layout. It is the same as calling the findViewById() method. The view id can be set in the annotation parameter, ie @ViewById(R.id.myTextView). If the view id is not set, the name of the field will be used. The field must not be private.
@ViewById 注解标识一个activity字段应该和布局中的一致的视图组件绑定在一起。就和调用 findViewById()方法一样。view id可以在注解参数中添加,比如 @ViewById(R.id.myTextView)。假如没有设置view id,将默认使用字段名。这个字段不能是private类型的,一般使用默认类型或者protected类型。
点击事件也可以很方便了,甚至于可以写在xml文件里。
package com.example.androidannotations;
import android.app.Activity;
import android.text.Editable;
import android.widget.EditText;
import android.widget.TextView;
import com.googlecode.androidannotations.annotations.AfterTextChange;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.Fullscreen;
import com.googlecode.androidannotations.annotations.NoTitle;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.res.StringRes;
//Eactivity注释可以设置Layout,相当于setConentView方法
@EActivity(R.layout.activity_main)
@Fullscreen
@NoTitle
public class MainActivity extends Activity {
//ViewById注释功能与findViewById相同,如果声明的变量名就是id,可以省去参数,否则应加上id,如ViewById(R.id.tv)
@ViewById
TextView tv;
@ViewById
EditText edit;
@StringRes(R.string.hello_world)
String hello;
//需要使用@Bean标签
@Bean
Student stu;
//AfterViews注释定义的方法会在OnCreate方法的setContentView后执行
@AfterViews
void init()
{
tv.setText("asfsdf");
}
//在EditText内容改变时,更新TextView内容
@AfterTextChange(R.id.edit)
void afterEditChange(Editable text, TextView hello)
{
tv.setText(hello.getText());
}
//点击TextView时调用
@Click(R.id.tv)
void tvClicked()
{
stu.Toast();
stu.backThread();
}
//在stu中也可以回调这个方法来更新UI
@UiThread
public void updateTv(int i)
{
tv.setText(String.valueOf(i));
}
}
@EActivity
- @App
- BookmarkApplication application;
- @RestService
- BookmarkClient restClient;
- @AnimationRes
- Animation fadeIn;
- @SystemService
- ClipboardManager clipboardManager;