EasyAnnotation框架用法介紹

如何成爲T型人才,垂直在一個行業中,必需要有一整套知識體系,在這裏,就一個字堅持~

前言

EasyAnnotation框架使用編譯時註解,解決項目重複功能代碼編碼,經過注APT技術生成樣板代碼,在經過註解進行注入,提升開發效率。該框架主要功能模塊:一、對象工廠模式實例化。二、界面跳轉。三、控件綁定、事件綁定功能。git

Contents

1、功能介紹

註解 描述
@BindView 指定控件綁定
@OnClick 指定事件綁定
@IntentField 指定Activity跳轉時,Binder數據傳輸參數(範圍:成員變量)
@IntentParameter 指定Activity跳轉時,Binder數據傳輸參數(範圍:方法形參)
@IntentMethod 指定Activity跳轉目標類名
@ObjectFactory 指定生成對象工廠類

1.一、使用數據綁定功能

  1. 初始化註解,須要在Activity初始化方法onCreate,調用AnnotationUtils.bind(this)進行注入。github

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_three);
         AnnotationUtils.bind(this);
    }
    複製代碼
  2. View控件綁定使用@BindView(id)注入。markdown

    @BindView(R.id.mTvTitle)
    public TextView mTvTitle;
    複製代碼
  3. View控件事件綁定使用@OnClick([id])注入。框架

    @OnClick({R.id.mBtSubmit})
    public void onClick(View view){
       if(view.getId()==R.id.mBtSubmit){
           Toast.makeText(this,"點擊了",Toast.LENGTH_SHORT).show();
       }
    }
    複製代碼

1.二、使用Activity之間跳轉功能

1.2.一、使用成員變量設置Activity中binder參數,@IntentField(目標Activity類名)、@IntentMethod(目標Activity類名)
  1. Activity傳遞數據,並進行跳轉。ide

    public class OneActivity extends AppCompatActivity {
        @IntentField("TwoActivity")
         String title = "OneActivity";
    
        @IntentField("TwoActivity")
         UserInfo userInfo;
        
         @IntentMethod("TwoActivity")
         public void jumpOneToTwoActivity() {
              userInfo = new UserInfo("username", "password");
              new TwoActivity$Jump().jumpActivity(OneActivity.this, title, userInfo);
         }
    
    }
    複製代碼
  2. Activity接收數據。oop

    public class TwoActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_two);
            initData();
            initView();
        }
    
        /**
         * 獲取數據
         */
        private void initData() {
            TwoActivity$Init activity = (TwoActivity$Init)
                    new TwoActivity$Init().initActivity(this);
            String title = activity.title;
            UserInfo userInfo = activity.userInfo;
            Toast.makeText(this, "title=" + title + ";username=" +
                            userInfo.username + ";password" + userInfo.password,
                    Toast.LENGTH_LONG).show();
        }
     }
    複製代碼
1.2.二、使用方法形參設置Activity中binder參數,@IntentParameter(目標Activity類名)、@IntentMethod(目標Activity類名)
  1. Activity傳遞數據,並進行跳轉。this

    @IntentMethod("MainActivity")
    public void jumpOneToMainActivity(
    									@IntentParameter("MainActivity") String name,
                                        @IntentParameter("MainActivity") int count,
                                        String title) {
            new MainActivity$Jump().jumpActivity(OneActivity.this, name, count);
        }
    複製代碼
  2. Activity接收數據。編碼

    private void initData() {
            MainActivity$Init activity = (MainActivity$Init)
                    new MainActivity$Init().initActivity(this, 0);
            int count = activity.count;
            String name = activity.name;
            Toast.makeText(this,
                    "count=" + count + ";name=" + name, Toast.LENGTH_SHORT).show();
        }
    複製代碼

1.三、使用生成對象工廠類

  1. @ObjectFactory(type=工廠類,key="類標識"),生成實例化對象。spa

    public abstract class BaseFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(getLayoutId(), null);
            return rootView;
        }
    
        public abstract int getLayoutId();
    }
    複製代碼
    @ObjectFactory(type = BaseFragment.class, key = "Home")
    public class HomeFragment extends BaseFragment {
        @Override
        public int getLayoutId() {
            return R.layout.fragment_home;
        }
    }
    複製代碼
  2. 經過標識獲取實例化對象。code

    private void initView() {
            HomeFragment main = (HomeFragment)
                    new BaseFragment$Factory().objectFactory("Home");
    }
    複製代碼

About me

相關文章
相關標籤/搜索