Roboguice学习之视图注入

Robuguide的使用

准备工作:

 

首先在项目中必须实现GreetingModule和RoboguiceDemoApplication

GreetingModule.java

import com.google.inject.AbstractModule;

 

public class GreetingModule extends AbstractModule {

 

    @Override

    protected void configure() {

    }

 

}

RoboguiceDemoApplication.java

import roboguice.application.RoboApplication;

 

public class RoboguiceDemoApplication extends RoboApplication{

 

}

在values文件夹下必须出现如下文件:

 

Roboguice.xml文件中item对应工程中对应的GreetingModule

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="roboguice_modules">

        <item>com.pstreets.roboguice.demo.GreetingModule</item>

    </string-array>

</resources>

视图注入:

Activity必须继承实现RoboActivity

Demo如下:

public class InjectViewDemo extends RoboActivity {

   

    @InjectView (R.id.button) Button goButton;

    @InjectView (R.id.textview1) TextView textView1;

    @InjectView (R.id.textview2) TextView textView2;

    @InjectView (R.id.textview3) TextView textView3;

    @InjectView (R.id.textview4) TextView textView4;

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     

     setContentView(R.layout.injectview);

         goButton.setOnClickListener(mGoListener);

     }

 

     private OnClickListener mGoListener = new OnClickListener()

     {

         public void onClick(View v)

         {

         textView1.setText("Clicked");

         textView2.setText("Clicked");

         textView3.setText("Clicked");

         textView4.setText("Clicked");

         }

     };

}

视图界面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

 

  <TextView 

    android:id="@+id/textview1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/injectview"

    />

   

   <TextView 

    android:id="@+id/textview2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/injectview"

    />

   

    <TextView 

    android:id="@+id/textview3"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/injectview"

    />

   

    <TextView 

    android:id="@+id/textview4"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/injectview"

    />

   

   <Button android:id="@+id/button"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_gravity="center_vertical"

            android:text="@string/clickmebutton"/>

   

</LinearLayout>

 

posted @ 2014-04-10 09:40  西北野狼  阅读(241)  评论(0编辑  收藏  举报