`

xUtils3.0的使用(一)IOC

阅读更多
首先,没啥说的,放上xUtils的github地址:
https://github.com/wyouflf/xUtils3


作者的原话:xUtils3 api变化较多;
xUtils 2.x对Android 6.0兼容不是很好, 请尽快升级至xUtils3;
xUtils 最低兼容Android 4.0 (api level 14);(各位同学记得要留意自己项目的minsdk,低于14运行会报错的)
数据库api简化提高性能, 达到和greenDao一致的性能.
......


本文浅谈一下IOC的用法

一.使用前配置
需要的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

初始化:
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); 
        //是否输出debug日志,开启debug会影响性能.(这行代码可要可不要)
    }
}


写好MyApplicaton后还要在AndroidManifest.xml中通过android:name属性指定这个application:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 </application>


二.开始使用
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    @ViewInject(R.id.fab)
    private FloatingActionButton button;
    //这里注意,变量和方法一定要是私有的(private)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //类似开启xUtils注入的功能
    }
    @Event(R.id.fab)   //参数支持数组 value={id1, id2, id3}
    private void clickMe(View view){
        Snackbar.make(view,"ioc succeed!",Snackbar.LENGTH_SHORT).show();
    }
}


总体来说,使用方法和ButterKnife相似。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics