前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Kotlin 程序设计》第十四章 使用Kotlin开发Android程序

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

作者头像
一个会写诗的程序员
发布2018-08-17 17:51:08
1.3K0
发布2018-08-17 17:51:08
举报
文章被收录于专栏:一个会写诗的程序员的博客

第十四章 使用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的选项上面勾一下就可以了。

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

代码语言:javascript
复制
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

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

FuckGoodsComponent

代码语言:javascript
复制
父Component为ApiComponent 用于注入FuckGoodsPresenter
 
 依赖于: FuckGoodsModule(提供FuckGoodsView)

RandomComponent

代码语言:javascript
复制
父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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.05.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第十四章 使用Kotlin开发Android程序
  • Kotlin for android
  • Kotlin生态库
    • 项目模式
      • Kotlin
        • MVP
          • Dagger2
            • Rxjava + Retrofit + okhttp3
              • DeepLinkDispatch
                • GSON
                  • Glide
                    • ByeBurGer
                    • 参考资料
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档