美文网首页Kotlin 程序设计Kotlin程序设计
《Kotlin 程序设计》第十四章 使用Kotlin开发Andr

《Kotlin 程序设计》第十四章 使用Kotlin开发Andr

作者: 光剑书架上的书 | 来源:发表于2017-05-30 17:55 被阅读86次

    第十四章 使用Kotlin开发Android程序

    正式上架:《Kotlin极简教程》Official on shelves: Kotlin Programming minimalist tutorial
    京东JD:https://item.jd.com/12181725.html
    天猫Tmall:https://detail.tmall.com/item.htm?id=558540170670

    Kotlin for android

    https://github.com/EasyKotlin/Kotlin-for-Android-Developers

    展示一个实现登录注册的demo

    下面就我们就开始一个入门级别的demo吧,现在谷歌已经推出了android studio3.0已经支持了Kotlin这门语言,下载地址:https://developer.android.google.cn/studio/preview/index.html ,只需要在这里新建一个工程,然后在是否要加入kotlin的选项上面勾一下就可以了。

    下面看一下登录注册的代码:

    class MainActivity : AppCompatActivity() {
    
        var userName: EditText? = null
        var userPwd: EditText? = null
        var register: Button? = null
        var login: Button? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            userName = findViewById(R.id.user_name) as EditText
            userPwd = findViewById(R.id.user_pwd) as EditText
    
            register = findViewById(R.id.register) as Button
            login = findViewById(R.id.login) as Button
    
            login!!.setOnClickListener {
                if (userName!!.text.toString() == "123456" && userPwd!!.text.toString() == "abc") {
                    Toast.makeText(this, "login succeed1", Toast.LENGTH_SHORT).show()
                    val intent = Intent(this,HomeActivity::class.java)
                    startActivity(intent)
                }
            }
    
            register!!.setOnClickListener {
                Toast.makeText(this, "the function has not open ...", Toast.LENGTH_SHORT).show()
            }
    
        }
    
    }
    
    
    

    当然实现的代码就非常简单啦,只是可能我们在刚开始接触这门语言的时候有一些的不理解。大家可以看一下上面的代码,要是有什么不理解的地方欢迎issue。

    源码地址:https://github.com/linsir6/Kotlin

    https://github.com/EasyKotlin/Bandhook-Kotlin

    Kotlin生态库

    项目模式

    Kotlin

    让你的代码量大大减少,函数式编程让你爽到飞上天!如果你想学习Kotlin,本项目应该会给你不少帮助。

    MVP

    通过契约类Contract管理View Model Presenter接口。

    • Model -- 主要处理业务,用于数据的获取(如网络、本地缓存)。
    • View -- 用于把数据展示,并且提供交互。
    • Presenter -- View和Model交互的桥梁,二者通过Presenter建立联系。

    主要流程如下: 用户与View交互,View得知用户需要加载数据,告知Presenter,Presenter则告知Model,Model拿到数据反交于Prsenter,Presenter将数据交给View进行展示。

    Dagger2

    项目中,主要进行presenter、model、retrofit Api等类的注入操作。

    ApiComponent

    主Component、用于注入AppComponent、便于提供子Component依赖。
     
     依赖于:
     
     1.ApiModule(提供okhttpClient、Retrofit、Api等)
     
     2.AppModule(提供context对象(okhttp拦截器所需))
    

    FuckGoodsComponent

    父Component为ApiComponent 用于注入FuckGoodsPresenter
     
     依赖于: FuckGoodsModule(提供FuckGoodsView)
    

    RandomComponent

    父Component为ApiComponent 用于注入RandomPresenter
    
    依赖于 : RandomModule(提供RandomView) 
    

    Rxjava + Retrofit + okhttp3

    主要用于网络访问。

    DeepLinkDispatch

    基于路由进行页面转发。

    GankClientUri 定义一些路由规则、URI等

    GankRouter 统一由此进行路由操作

    GSON

    用于json的解析操作。

    Glide

    用于图片的加载。

    ByeBurGer

    用于导航栏以及悬浮按钮滑动隐藏。

    参考资料

    1.http://git.bookislife.com/post/2016/dev-android-using-scala/
    2.https://github.com/saturday06/gradle-android-scala-plugin
    3.https://github.com/pocorall/scaloid

    相关文章

      网友评论

        本文标题:《Kotlin 程序设计》第十四章 使用Kotlin开发Andr

        本文链接:https://www.haomeiwen.com/subject/fvmrfxtx.html