日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Dagger简单Demo

發布時間:2025/3/21 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dagger简单Demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Dagger實現依賴注入,為了解決程序直接的耦合度。
本例子主要為了實現簡單的依賴注入

配置:
Project級別的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:2.1.2'classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files} }allprojects {repositories {jcenter()} }task clean(type: Delete) {delete rootProject.buildDir }

Module級別的build.gradle

apply plugin: 'com.android.application' apply plugin: 'android-apt'android {compileSdkVersion 23buildToolsVersion "23.0.3"defaultConfig {applicationId "com.android.demo"minSdkVersion 17targetSdkVersion 23versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}} }dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:23.4.0'compile 'com.jakewharton:butterknife:8.0.1'apt 'com.jakewharton:butterknife-compiler:8.0.1'compile 'com.squareup.dagger:dagger:1.2.5'apt 'com.squareup.dagger:dagger-compiler:1.2.5' }



布局

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><TextView android:id="@+id/message"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Button android:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/message"android:layout_margin="10dp"android:text="開始"/> </RelativeLayout>



Student.java

package com.android.demo;/*** 學生*/ public class Student {String id;String name;public void setId(String id) {this.id = id;}public void setName(String name) {this.name = name;}public String getId() {return id;}public String getName() {return name;}public Student(){}}


AppModule.java

package com.android.demo;import javax.inject.Singleton;import dagger.Module; import dagger.Provides;@Module(injects = {MainActivity.class},complete = false ,library = true) public class AppModule {@Provides @Singletonpublic Student provideStudent(){Student student = new Student();student.setId("2014");student.setName("WiseClown");return student;} }


package com.android.demo;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView;import javax.inject.Inject;import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import dagger.ObjectGraph;public class MainActivity extends AppCompatActivity {@InjectStudent student;@BindView(R.id.message)TextView message;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ObjectGraph objectGraph = ObjectGraph.create(AppModule.class);objectGraph.inject(this);ButterKnife.bind(this);}@OnClick(R.id.btn)public void submit(View view){message.setText(student.getId()+"\n"+student.getName());}}

分析:
在MainActivity中需要Student類的實例,所以通過依賴注入器將Student類在AppModule中實例化后再注入MainActivity中。

總結

以上是生活随笔為你收集整理的Dagger简单Demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。