Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin+Android

Paul Barré
December 08, 2017

 Kotlin+Android

2012/12/8
フロントエンド勉強会 in 山陰 #03
KotlinでのAndroid開発の紹介

2017.12.21時点の修正
P.8
fun add(x: Int, y: Int): Int {
return x + y
}

Paul Barré

December 08, 2017
Tweet

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ ➤ όϨɹϙʔϧ ➤ ϑϥϯεग़਎ ➤ ೔ຊࡏॅ̔೥໨ʢ౦ژɺٶ࡚ɺদߐʣ ➤ גࣜձࣾιχοΫϜʔϒɹౡࠜࣄۀॴɺϑϩϯτΤϯδχΞ ➤

    ϞόΠϧ։ൃܦݧ͸̍೥ʢiOSɺAndroidʣ ➤ ϒϩάɿhttp://paulbarre.co ➤ Githubɿhttps://github.com/paulbarre 3
  2. KOTLINͱ͸ ➤ KotlinʢίτϦϯʣ ➤ JetBrainsࣾ (PHPStorm, IntelliJ IDEA…ʣ
 ͕։ൃͯ͠Δ ➤

    JVM্Ͱಈ࡞͢Δ ➤ ੩తܕ෇ͷOOPݴޠ ➤ ؔ਺ܕݴޠ ➤ Φʔϓϯιʔε
 Kotlinݴޠͷϩΰ ϑΟϯϥϯυ࿷ʹ͋ΔίτϦϯౡʢϩγΞʣ 4
  3. KOTLINͷཤྺ ➤ 2010೥ɹ։ൃͷ͸͡Ί ➤ 2011೥7݄20೔ɹൃද ➤ 2012೥2݄14೔ɹApache 2.0ϥΠηϯεʹج͍ͮͯΦʔϓϯ ιʔεԽ ➤

    2016೥2݄15೔ɹKotlin v1.0ϦϦʔε ➤ 2017೥5݄17೔ɹGoogle I/OͰAndroidΞϓϦ։ൃʹ౰ͨͬͯ ୈҰڃݴޠͷҰͭબఆ 5
  4. جຊͷؔ਺ fun add(x: Int, y: Int): Int { fun x

    + y } fun add(x: Int, y: Int): Int = x + y fun add(x: Int, y: Int) = x + y 8
  5. NULL҆શ class MyClass() { fun doSomething() { … } }

    val a: MyClass = null //ίϯύΠϧΤϥʔ val b: MyClass? = null b.doSomething() //ίϯύΠϧΤϥʔ 9
  6. WHEN fun cases(obj: Any) { when (obj) { 1 ->

    println("One") "Hello" -> println("Greeting") is Long -> println("Long") !is String -> println("Not a string") else -> println("Unknown") } } 10
  7. ֦ுؔ਺ fun <T> MutableList<T>.swap(index1: Int, index2: Int) { val tmp

    = this[index1] this[index1] = this[index2] this[index2] = tmp } 11
  8. SWIFTͱҧ͏͚ͲɺࣅͯΔͱ͜Ζ͕͋Δ // Swift var, let func foo(arg: Any) -> Any

    class MyClass {} var ins = MyClass() ins.add(x: 1, y: 2) // Kotlin var, val fun foo(arg: Any): Any class MyClass {} var ins = MyClass() ins.add(1, 2) 13
  9. SWIFTͱҧ͏͚ͲɺࣅͯΔͱ͜Ζ͕͋Δ // SwiftͷOptional let ins2: MyClass? ins2?.foo() if let ins2

    = ins2 { ins2.foo() } // KotlinͷNullable val ins2: MyClass? ins2?.foo() if (ins2 != null) { ins2.foo() } 14
  10. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛσʔλΫϥε 15 data class User(val name: String, val age: Int)

    ➤ getters / settersʢvarͷ৔߹ʣ ➤ equals ➤ hashCode ➤ toString
 
 ্هͷؔ਺͸ࣗಈͰ࡞ͬͯ͘ΕΔ
  11. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 16 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener(object : OnClickListener { override fun onClick(v: View) { toast("Click") } })
  12. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 17 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener({ view -> toast(“Click”) })
  13. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 18 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener({ toast(“Click”) })
  14. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 19 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener { toast(“Click”) }
  15. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛANDROID EXTENSION 20 // Java TextView view = (TextView) findViewById(R.id.hello);

    view.setText(“Hello!”); // Kotlin import kotlinx.android.synthetic.<layout>.* …
 hello.text = “Hello!”
  16. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛANKO ➤ AnkoɿਓؾͷϥΠϒϥϦ
 https://github.com/Kotlin/anko ➤ Anko Intent 23 val intent

    = Intent(this, SomeOtherActivity::class.java) intent.putExtra("id", 5) intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP) startActivity(intent) startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
  17. KOTLINͱؾʹͳͬͨͱ͜Ζ ➤ ৚݅ԋࢉࢠ͕ͳ͍ ➤ SwiftΈ͍ͨͷguard let͕ͳ͍ ➤ let͸Swiftͱͷҧ͏
 ➤ JavaΑΓϏϧυ͕࣌ؒ஗͍


    https://medium.com/keepsafe-engineering/kotlin-vs-java- compilation-speed-e6c174b39b5d ➤ JavaΑΓύοέʔδϑΝΠϧͷαΠζ͕େ͖͍ 24 val user = findUser(id) user?.let { foundUser -> }