让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程

 

写在前面

使用kotlin开发android已经两周多了。得到的好处太多,敲代码简直是爽的飞起。kotlin简单,易用,代码量少。这么爽的东西,不敢私藏,所以写出来与大家分享。

可能标题说减少3倍你不信,但事实上并不夸张。通过学习本系列文章,你会发现,代码。真的少了很多。
本系列文章将会使用kotlin从无到有撸一个android小应用程序(不是微信小程序= =!),而并非传统的学习语言方式(如:《21天精通kotlin》、《kotlin从入门到精通》)。在创作的过程中学习语法,我认为单纯的学语法没什么意义,学了也会忘。因为本身不是为了学而学,而是为了用而学。用得多了,也就记住了。

至于Kotlin是什么,简单的说就是跑在jvm上的新语言,他的特点就是短小 ~~快~~,至于有什么好处,那就说不完啊,挑特点来说。

  1. 无缝对接java,可以java代码和kotlin代码互相调用
  2. 一键java转kotlin,如果你有遗留的java代码,可以一键转换
  3. 函数扩展功能,再也不知道啥叫继承

创建第一个kotlin应用!

  1. 首先打开你的AS,像往常一样,创建一个Android应用。

  2. 然后在Setting里找到Plugin选项,搜索kotlin插件。从官方仓库下载安卓即可。

  3. 在app的gradle下加入如下代码

  1. 在project的gradle下加入如下代码(注意你自己配置的时候,改成最新版本)

  1. 接下来切换到MainActivity 同时按下cmd + alt + shift + k (mac下),即可把java代码转换成kotlin代码。

分析转换的代码

转换后

将两断代码做一个比较,可以看到的语法变化有:

  1. extends 变成了 :
  2. 函数定义关键字变为了fun
  3. 分号不见了
  4. 变量名称写在了类型前面
    这些是语法的变化,到后面还会有一些,用得多了,也就记住了。后面再遇到语法变化还会继续总结。

将Hello World编为Hello Kotlin

首先给xml文件里的textView加一个id

在以往的java代码中,想要改变textview的文本,需要先findviewbyid()之后再setText(),控件多了就很臃肿。是时候试试kotlin了!

下面用kotlin实现同样的功能,代码如下:

纳尼????就一句话??? 卧槽??? 什么鬼????

额。。你冷静一下,我知道你内心很卧槽。但确实是这样,如你看到的,findviewbyid不见了,取而代之的是直接写id。kotlin可以直接根据id找到控件,而避免findviewbyid,这时候你需要注意一下import头,导入这个东西才可以

另外,setText也不见了。这是kotlin的一个特性,所有getter和setter可以省略,直接变为 .text

感觉如何?代码量是不是减少了许多,实际上这只是凤毛麟角而已啦。
本文这就结束了(嫌文字太短?不要急啦,你先去动手创建个工程咯。。系列文章,既然挖了坑,就不会拖更的(吧..))

下一篇:

让你的代码量减少3倍!使用kotlin开发Android(二) –秘笈!扩展函数

 

本项目使用的代码地址
欢迎加入Kotlin小乐园群:479109923

如果你是Android开发者,那么你还可以来 wing的酒馆: 425983695 来分享你的开发经验哦

15 thoughts on “让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程

  1. Imtiyaz Nazki, a researcher on the Sher-e-Kashmir College of
    Agricultural Sciences and Expertise, stated flower cultivation offers 15
    instances more profit than traditional farming, and requires far much less land.

  2. Upcoming New Releases: Going wioth exactly the same concept above, if you have a buddy in radio or are merely really
    ahead of the curve musically, you could produce a site that lists and describes the upcoming new album releases.

    Anyone caan compose and talk about any topic, from hobbies to political vies.
    In internet affiliate marketing, people to some websige is drivsn from another website or from
    email.

  3. I am working out your small business plan today using
    a team of folks and that wwe project until this business will grow large for us.
    In this article I want to discuss something that has personally brought lots of money into my pocket
    witin the years. Thee most succdssful industry coaches and mentors in leadersjip training and marketing teach this golden nugget:
    People buy you first.

  4. We are actually watching the show for a few years, so we are certainly not that great at dancing.
    The massive amount styles and shapes intended for banquet tables offer endless the possiblility to decorate tables
    in the special way and earn the big event truly special and unique.
    A little patience might reward you with a nice picture
    of the frog or possibly a dragon fly.

  5. You may also build an income with your blogs by repackaging its content so that you could
    make money using it. By recognizing this, you can tweak posts or pages and perhaps give visitors another road to take.
    Normally, these would contain dummy text to provide you a perception the way the finished font side of the card you are creating.

  6. Do youu love it regular posting when you muxt keep regular content in your blog if you wish to commercialize it.

    Turn comments on and let people comment to have feedback in the public.

    Irefer to this as outstanding results, nevertheless, yyou ought to know that your videos can produce the identical most current listings for you also.

  7. Well there are 2 simple ways that this can be achieved,
    the foremost is from your browssr witholut the
    need tto purchase a special software as well as tthe other metghod is to download
    an exclusve software andd participate inn it from
    the desktop, are both very easy and straightforward ways,
    and even in case you have chosen the download method don’t get worried
    as it will as simple because it can be to download,
    install and play internet poker from your special application that you have just installed.
    It is like they are in opposition to gambling and so are limiting the wagyer until very sure
    they’ll win. Again, according to your results of that event, find
    out how you’ll be abe to adjust the strategies to produce your plan better.

发表评论

电子邮件地址不会被公开。