Andorid之Annotation框架初使用(二)

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;
}


在布局中使用Fragment:

<fragment
        // i only display name property , you should add more
        android:name="com.company.MyFragment_" />


代码中使用:

MyFragment fragment = new MyFragment_();


如果需要重写onCreateView方法,你应该:

@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }
}


@FragmentArg

@EFragment
public class MyFragment extends Fragment {
  @FragmentArg("myStringArgument")
  String myMessage;
  @FragmentArg
  String anotherStringArgument;
  @FragmentArg("myDateExtra")
  Date myDateArgumentWithDefaultValue = new Date();
}

MyFragment myFragment = MyFragment_.builder()
  .myMessage("Hello")
  .anotherStringArgument("World")
  .build();

 

BroadcastReciver:

@EReceiver
public class MyReceiver extends BroadcastReceiver {}


ContentProvider:

@EProvider
public class MyContentProvider extends ContentProvider {}


Service:

@EService
public class MyService extends Service {}


自定义View:

@EView
public class CustomButton extends Button {
        @App
        MyApplication application;
        @StringRes
        String someStringResource;
    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}


布局中使用自定义View:

<com.androidannotations.view.CustomButton_
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


代码中使用自定义View:

CustomButton button = CustomButton_.build(context);
posted on 2013-06-22 15:44  lee0oo0  阅读(1069)  评论(0编辑  收藏  举报