public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (TextView) findViewById(R.id.name); thumbnail = (ImageView) findViewById(R.id.thumbnail); loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); icon = getResources().getDrawable(R.drawable.icon); myName = getString(R.string.app_name); name.setText( "Hello, " + myName ); } }使用RoboGuice:
@ContentView(R.layout.main) class RoboWay extends RoboActivity { @InjectView(R.id.name) TextView name; @InjectView(R.id.thumbnail) ImageView thumbnail; @InjectResource(R.drawable.icon) Drawable icon; @InjectResource(R.string.app_name) String myName; @Inject LocationManager loc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); name.setText( "Hello, " + myName ); } }
dependencies { provided 'org.roboguice:roboblender:3.0' compile 'org.roboguice:roboguice:3.0' }
public class MyActivity extends RoboActivity { @InjectView(R.id.text1) TextView textView; @Override protected void onCreate( Bundle savedState ) { setContentView(R.layout.myactivity_layout); textView.setText("Hello!"); } }@ContentView注解:
@ContentView(R.layout.myactivity_layout) public class MyActivity extends RoboActivity { @InjectView(R.id.text1) TextView textView; @Override protected void onCreate( Bundle savedState ) { textView.setText("Hello!"); } }
@InjectView TextView commentEditText; @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); commentEditText.setText("Some comment"); }
public class MyActivity extends RoboActivity { @InjectResource(R.anim.my_animation) Animation myAnimation; }
class MyActivity extends RoboActivity { @Inject Vibrator vibrator; @Inject NotificationManager notificationManager;
class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo(); }当然如果不想使用默认构造器,可以注解构造器:
class Foo { Bar bar; @Inject public Foo(Bar bar) { this.bar = bar; } }也可以注解POJO的Field:
class Foo { @Inject Bar bar; // Roboguice doesn't need a constructor, but you might... }
@Singleton //a single instance of Foo is now used though the whole app class Foo { }
class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo(); }注意:这个单例一旦创建会一直驻留内存,其他类似的还有@ContextSingleton,@FragmentSingletion,他们的作用范围比较小,利于垃圾回收。
public interface IFoo {} public class Foo implements IFoo {}
public class MyActivity extends RoboActivity { //How to tell RoboGuice to inject an instance of Foo ? @Inject IFoo foo; }
<application ...> <meta-data android:name="roboguice.modules" android:value="com.example.MyModule,com.example.MyModule2" /> </application>2.继承AbstractModule
public class MyModule extends AbstractModule { //a default constructor is fine for a Module public void bind() { bind(IFoo.class).to(Foo.class); } }
public class MyModule2 extends AbstractModule { //if your module requires a context, add a constructor that will be passed a context. private Context context; //with RoboGuice 3.0, the constructor for AbstractModule will use an `Application`, not a `Context` public MyModule( Context context ) { this.context = context; } public void bind() { bind(IFoo.class).toInstance( new Foo(context)); } }
public class MyService extends RoboService { @Inject ComputeFooModule computeFooModule;
public class MyBroadcastReceiver extends RoboBroadcastReceiver { @Inject ComputeFooModule computeFooModule;
class MyView extends View { @Inject Foo foo; @InjectView(R.id.my_view) TextView myView; public MyView(Context context) { inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this);//注意这一行,为了使其正常工作。 } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); inflate(context,R.layout.my_layout, this); RoboGuice.getInjector(getContext()).injectMembers(this); } @Override public void onFinishInflate() { super.onFinishInflate(); //All injections are available from here //in both cases of XML and programmatic creations (see below) myView.setText(foo.computeFoo()); } }
public class MyActivity extends RoboActivity { // Any method with void return type and a single parameter with @Observes annotation // can be used as an event listener. This one listens to onResume. public void doSomethingOnResume( @Observes OnResumeEvent onResume ) { Ln.d("Called doSomethingOnResume in onResume"); } // As you might expect, some events can have parameters. The OnCreate event // has the savedInstanceState parameter that Android passes to onCreate(Bundle) public void doSomethingElseOnCreate( @Observes OnCreateEvent onCreate ) { Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState()) } // And of course, you can have multiple listeners for a given event. // Note that ordering of listener execution is indeterminate! public void xxx( @Observes OnCreateEvent onCreate ) { Ln.d("Hello, world!") } }
原文地址:http://blog.csdn.net/xbynet/article/details/45420533