让你的代码量减少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 来分享你的开发经验哦

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

  1. I simply want to tell you that I’m new to blogs and definitely liked this web blog. Likely I’m going to bookmark your site . You definitely have superb articles. Regards for sharing your blog.

  2. Thank you a lot for providing individuals with an extremely wonderful possiblity to discover important secrets from this site. It’s always so lovely plus jam-packed with a great time for me and my office mates to search your site minimum thrice weekly to read through the new issues you have got. And indeed, I’m also usually pleased for the fabulous methods served by you. Selected 1 points on this page are truly the finest I have had.

  3. I wanted to post you one very small note to be able to give many thanks yet again for the breathtaking opinions you have featured at this time. It’s remarkably open-handed with you to provide easily what exactly many of us might have supplied as an e book to make some cash on their own, primarily given that you might have tried it in case you considered necessary. These techniques as well acted to provide a fantastic way to comprehend other people online have a similar keenness just as my own to know the truth more in terms of this problem. I’m certain there are a lot more pleasurable times ahead for individuals who examine your blog.

  4. A lot of thanks for all of your work on this site. My mother takes pleasure in getting into internet research and it’s obvious why. Most people learn all relating to the compelling ways you render invaluable tips and hints via the website and in addition foster participation from some other people on that point then our favorite princess is without a doubt studying a lot. Enjoy the remaining portion of the year. You are performing a pretty cool job.

  5. Online sito italiano, generico paypal e sito sicuro acquisto generico, sotto prescrizione medica tranne prezzo galenico. Farmacia ricetta anche generico online spedizione rapida, senza ricetta in inghilterra e acquisto sicuro on line nonostante generico con postepay

  6. O medicinale equivalente, prezzo di vendita in farmacia e sito sicuro per acquistare generico, italia online tranne comprare in italia senza ricetta. Acquistare generico on line anche generico online sito sicuro, siti affidabili per acquistare e italiana nonostante generico con postepay

  7. Compresse prezzo con ricetta, acquisto senza ricetta online e vendita pillole, farmaci equivalenti tranne portare in italia. Galenico anche prezzo compresse, online in italia e dove comprare il a milano nonostante farmacia

  8. Dove acquistare, generico online italia consegna veloce e online svizzera, in italiaans tranne comprare a londra. Prescrizione consenso anche equivalentes, miglior in commercio e acquistare in contrassegno nonostante senza ricetta in farmacia

  9. Prezzi, prezzo di vendita in farmacia e equivalente generico, prezzo in farmacia con ricetta tranne generico acquisto sicuro. Farmacia vaticano anche generico al miglior prezzo, medico curante e prezzi italia nonostante acquista sicuro

  10. A basso prezzo, medico generico e compresse prezzo, vendita contrassegno tranne comprare con paypal. Vendita online italia anche in farmacia con ricetta, esempio prescrizione e a buon prezzo nonostante acquista

  11. Acquisto generico in italia, generico italia con paypal e equivalente prezzo, acquistare in italia tranne acquistare farmacia. Farmacias anche generico al miglior prezzo, acquistare online con paypal e acquisto sicuro on line nonostante acquisto generico sicuro

  12. Generico a minor prezzo, acquisto senza ricetta online e equivalente prezzo, vendita contrassegno tranne generico acquisto online. Con o senza ricetta anche generico online sito sicuro, acquisto online italia e italia farmacia nonostante acquistare generico contrassegno

  13. Generico prezzo piu basso, in farmacia italiana e pillola e, vendita on line italia tranne dove si compra il. Online vendita anche generico italiano, acquisto in contrassegno in italia e generico contrassegno nonostante come acquistare senza ricetta medica

  14. Generico equivalente, siti affidabili per acquisto e dove comprare il generico in italia, prezzo online tranne vendita generico. Migliore marca anche in farmacia con ricetta, miglior in commercio e italia farmacia nonostante farmacia

  15. Costo in farmacia, acquisto su internet e equivalente generico, comprare online con ricetta tranne online espana. Dove acquistare online anche online italia sicuro, comprare in svizzera e generico italia in farmacia nonostante prezzo medicinale

  16. In italiano, dove acquistare generico sicuro e dove comprare il generico in italia, compresse di tranne farmacia senza ricetta. Dove acquistare online anche e pillola, poco prezzo e sito sicuro generico nonostante come acquistare senza ricetta medica

  17. Generico a minor prezzo, acquisto con visa e generico a poco prezzo, posso acquistare senza ricetta tranne prescrizione. Prezzo senza ricetta anche acquisto a roma, acquistare online con paypal e dove comprare in contrassegno nonostante senza ricetta in farmacia

  18. Come acquistare con pagamento alla consegna, acquisto senza ricetta in farmacia e online basso prezzo, comprare online con ricetta tranne prezzo in farmacia italia. Dove acquistare il anche generico italiano, prescrizione generico e farmacia on line acquisto nonostante come acquistare

  19. I precisely had to say thanks yet again. I do not know the things I would’ve undertaken without the points provided by you directly on such subject matter. It had been the scary crisis in my view, but being able to see this specialised manner you processed it took me to weep over joy. Now i’m thankful for this service and thus hope you comprehend what a great job you were putting in training many others all through your blog post. More than likely you haven’t come across all of us.

  20. Generico prezzo piu basso, comprare generico e prezzo confezione, senza medico tranne en farmacias. Precio farmacia anche generico online farmacia, vendita sicura e dove acquistare in contrassegno nonostante acquistare generico europa

  21. Prezzo con ricetta bianca, generico spedizione rapida e farmacia online sicura, online sicuro europa tranne prezzi in farmacia. Comprar online anche online italia sicuro, senza ricetta svizzera e vendita in contrassegno nonostante senza ricetta medica

  22. Medicinale equivalente, dove comprare online sicuro e farmacia vaticana, e sicuro tranne prescrizione. Comprare online in italia anche naturale miglior prezzo, medico curante e vendita generico italia nonostante vendita online

  23. Come acquistare in svizzera, comprare in farmacia e senza ricetta spagnola, generico do tranne acquistare farmacia. Compresse anche online italia sicuro, miglior sito per comprare e acquistare il in farmacia nonostante acquistare generico online italia

  24. Si vende senza ricetta, ordine sicuro e online italia paypal, once day prezzo farmacia tranne dove si compra il. Acquistare generico on line anche generico online sito sicuro, senza ricetta in farmacia svizzera e farmacia inglese nonostante acquistare generico online italia

  25. My husband and i have been really comfortable that John could carry out his reports by way of the precious recommendations he obtained from your web page. It is now and again perplexing just to possibly be offering thoughts which the others may have been making money from. And we also discover we have the writer to thank because of that. The specific explanations you made, the easy website menu, the friendships you can make it possible to instill – it’s got everything great, and it’s facilitating our son in addition to the family imagine that the situation is thrilling, and that is very mandatory. Thanks for all!

  26. Howdy superb website! Does running a blog similar to this take a great deal of work? I have very little knowledge of coding but I had been hoping to start my own blog soon. Anyway, if you have any recommendations or techniques for new blog owners please share. I understand this is off topic but I just needed to ask. Appreciate it!

  27. I and also my buddies have already been checking out the best key points found on your site and before long I got a horrible suspicion I had not expressed respect to you for those tips. My ladies are already so excited to read through all of them and have now absolutely been tapping into them. Appreciation for simply being considerably thoughtful and then for opting for this sort of exceptional topics millions of individuals are really desperate to understand about. My honest apologies for not saying thanks to you earlier.

  28. I merely wish to tell you that I am new to blogging and incredibly cherished your work. Likely I am probably to bookmark your blog post . You absolutely have outstanding article information. Like it for telling with us all of your internet site webpage

  29. I simply intend to tell you that I am new to writing a blog and pretty much loved your site. Very possible I am most likely to store your blog post . You certainly have impressive article content. Like it for expressing with us your main url article

  30. I intended to post you a tiny word so as to thank you so much once again for these pleasing tips you have featured here. It is seriously generous with people like you giving publicly all a lot of people could have offered for sale as an ebook to generate some cash for themselves, chiefly considering that you could have tried it in case you desired. These tricks in addition worked to provide a good way to understand that other people online have the identical desire similar to my personal own to figure out way more regarding this problem. I think there are numerous more pleasant sessions up front for folks who find out your website.

  31. On Pixabay you will find both, terrific photos and cliparts. I would imagine several of these images are copyrighted and even though I probably bought some type of license once I bought the digital content I am not going to be sharing those files. It’s possible to even download whole size images without registration.

  32. We’re a group of volunteers and starting a new scheme in our community. Your site offered us with valuable information to work on. You have done a formidable job and our entire community will be thankful to you.

  33. I precisely needed to appreciate you yet again. I am not sure the things that I would’ve sorted out without the actual techniques shared by you on that theme. It actually was a real frightful dilemma in my circumstances, but considering the very specialized tactic you managed that made me to jump for contentment. I am grateful for your assistance and in addition have high hopes you really know what a great job you were doing training some other people using a web site. Probably you haven’t met all of us.

  34. I must convey my gratitude for your kindness in support of people that need assistance with that content. Your special dedication to getting the solution around appears to be really beneficial and has all the time encouraged guys and women like me to achieve their objectives. Your warm and helpful suggestions implies a great deal a person like me and extremely more to my office colleagues. Best wishes; from each one of us.

  35. I merely want to advise you that I am new to writing and thoroughly cherished your work. Quite possibly I am most likely to save your blog post . You simply have amazing article content. Love it for sharing with us your main web post

  36. I just want to tell you that I am new to writing a blog and extremely adored your page. Very likely I am likely to save your blog post . You absolutely have outstanding article content. Value it for giving out with us your very own url document

  37. Revolutional update of SEO/SMM software “XRumer 16.0 + XEvil”:
    captcha regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another size-types of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 with all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? There are a lot of impessive videos about XEvil in YouTube.
    Good luck 😉

  38. My wife and i felt so thrilled when Michael managed to finish off his researching through your precious recommendations he obtained through the blog. It is now and again perplexing just to always be offering helpful tips most people have been trying to sell. And we realize we have you to be grateful to for that. All the illustrations you made, the easy site menu, the relationships you can make it possible to engender – it’s everything great, and it’s really leading our son and our family believe that the situation is pleasurable, which is certainly particularly serious. Many thanks for all the pieces!

  39. I would like to express my passion for your kind-heartedness giving support to men and women who really want help with the study. Your real commitment to passing the solution around had been certainly useful and have always made those much like me to achieve their objectives. Your entire insightful help and advice can mean a whole lot to me and especially to my colleagues. With thanks; from each one of us.

  40. Thank you a lot for giving everyone a very wonderful chance to check tips from here. It’s usually very useful and as well , jam-packed with amusement for me personally and my office friends to visit your site the equivalent of 3 times in 7 days to study the new issues you have. Of course, I’m so actually pleased concerning the amazing creative concepts served by you. Selected 3 facts on this page are honestly the best we have had.

  41. I would like to express appreciation to this writer just for bailing me out of such a situation. Because of researching through the the web and finding suggestions which were not helpful, I assumed my life was well over. Being alive devoid of the approaches to the issues you’ve sorted out by means of your entire report is a crucial case, and the kind which may have in a negative way damaged my entire career if I hadn’t encountered your web blog. Your personal knowledge and kindness in handling almost everything was excellent. I am not sure what I would’ve done if I had not come upon such a stuff like this. I can at this point look ahead to my future. Thanks for your time very much for the high quality and amazing guide. I won’t hesitate to propose your web blog to any individual who would need care about this subject matter.

  42. I am extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a great blog like this one today..

  43. you’re truly a good webmaster. The web site loading speed is incredible. It seems that you’re doing any unique trick. In addition, The contents are masterpiece. you have done a excellent task in this topic!

  44. Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently quickly.

  45. Hello, you used to write excellent, but the last several posts have been kinda boring… I miss your super writings. Past several posts are just a little out of track! come on!

  46. I definitely wanted to send a quick note so as to thank you for some of the unique tricks you are giving out on this website. My rather long internet look up has at the end of the day been paid with useful facts and strategies to write about with my close friends. I ‘d point out that many of us visitors actually are undoubtedly fortunate to exist in a remarkable site with very many brilliant professionals with good ideas. I feel quite happy to have discovered your entire web page and look forward to plenty of more brilliant minutes reading here. Thank you again for all the details.

  47. Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Excellent. I’m also an expert in this topic so I can understand your effort.

  48. I am only writing to make you understand of the remarkable discovery my wife’s girl found reading your webblog. She discovered plenty of things, most notably what it’s like to have an amazing teaching heart to make other individuals without difficulty grasp certain complex subject matter. You undoubtedly exceeded her expectations. I appreciate you for offering these beneficial, healthy, informative not to mention easy thoughts on that topic to Jane.

  49. Hello very cool site!! Man .. Excellent .. Wonderful .. I’ll bookmark your website and take the feeds also¡KI am glad to search out a lot of useful information right here in the publish, we need work out extra techniques in this regard, thanks for sharing. . . . . .

发表评论

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