編輯:關於android開發
作為一個碼農,什麼都不用多講,貼代碼
build project
build module
Application
public class App extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent= DaggerAppComponent.builder() .appModule(new AppModule(this)) .build(); } public AppComponent getAppComponent() { return appComponent; } } View CodeAppComponent@Singleton @Component(modules = { AppModule.class }) public interface AppComponent { @ContextLife("Application") App getApplication(); RetrofitHelper retrofitHelper(); //提供http的幫助類 } View Code
AppModule@Module public class AppModule { private App application; public AppModule(App application){ this.application=application; } @Provides @Singleton @ContextLife("Application") public App provideApplication(){ return application; } @Provides @Singleton public RetrofitHelper provideRetrofitHelper() { return new RetrofitHelper(); } } View Code
ActivityComponent@ActivityScope @Component(dependencies = AppComponent.class, modules = ActivityModule.class)//關聯上下 public interface ActivityComponent { Activity getActivity(); void inject(LoginActivity loginActivity);//要在類中使用注入功能 void inject(MainActivity mainActivity); } View Code
ActivityModule@Module public class ActivityModule { private Activity mActivity; public ActivityModule(Activity activity) { this.mActivity = activity; } @Provides @ActivityScope public Activity provideActivity() { return mActivity; } @Provides @ActivityScope public IPresenterFactory providePresenter(RetrofitHelper retrofitHelper) { return new PresenterFactory(retrofitHelper); } } View Code
Base裡面使用注入,否則無法注入
public abstract class PresenterActivity extends BaseActivity { private final static String TAG = "PresenterActivity"; protected abstract void initInject(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initInject(); } protected ActivityComponent getActivityComponent(){ return DaggerActivityComponent.builder() .appComponent(App.getInstance().getAppComponent()) .activityModule(getActivityModule()) .build(); } private ActivityModule getActivityModule() { return new ActivityModule(this); } } View Code此時前提條件都有了
點擊運行自動生成注入代碼(沒問題的前提)
使用注入:
public class MainActivity extends PresenterActivity { @Inject protected IPresenterFactory mPresenter;//沒有創建對象,inject標簽注入類型 private final int BASIC_PERMISSION_REQUEST_CODE = 100; private TextView username; @Override public void findView() { username = findView(R.id.username); username.setText(App.getInstance().getUserEntity().getName()+" hello"); } @Override public void init() { if (mPresenter != null){ Toasts.showShort("mPresenter success",context);//注入成功會打印出success } } @Override public int layoutResID() { return R.layout.activity_main; } @Override protected void initInject() { getActivityComponent().inject(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent() { Toasts.showShort("刷新UI",context); } }
《第一行代碼》目錄 圖書目錄: 第1章 開始啟程,你的第一行Android代碼 1.1 了解全貌,Android王國簡介 1.1.1 Androi
仿Android印象筆記底部導航欄 最近用上了印象筆記,覺得android 版的底部導航欄挺不錯的,好多應用裡面都有用到,想著自己動手實現一下,不多說,先上圖:
在android中使用SQLite數據庫 SQLite數據庫以其輕量、體積小等特點,使其在開發中運用的非常廣泛,在前面的博客中我也介紹過在Cocos2d-x中使用SQ
三種方式獲得手機屏幕的寬和高,三種方式手機屏幕 主要代碼: 1 package com.km.screeninfo; 2 3