(ãã®ã¨ã³ããªã¼ã¯Kotlin Advent Calendar 2014 - Adventarã®21æ¥ç®ã®è¨äºã§ãï¼)
24æ³ããã®ã¾ã¾Kotlinããããã«20代å¾åã«è¶³ãè¸ã¿å
¥ãã¦ããã®ãèªåãã
ã¤ãã«Kotlinç«¥è²ãæ¨ã¦ãäºã«ãã¾ããã
ãã¨ãã¤ãã§ã«Androidç«¥è²ãæ¨ã¦ã¾ãã
しょぼちむ Advent Calendar 2014 - Adventarã®12æ¥ç®ã[twitter:@grimrose]ããã®
open build/reports/life/index.html: しょぼちむさんがKotlinをやらない理由を考えてみたã®è¨äºã«ãããâããã¦ãã£ãã®ã§ãã¨ããããGitHub - JetBrains/kotlin: The Kotlin Programming Languageã«ã¹ã¿ã¼ã¤ããã¨ãããå§ããã
ãããããæ»ãåºãã§ãã
宿é¡ã¯ããããã¾ã§ãããªãã¿ã¤ããªã®ã§ã21æ¥ããå§ãã¦ã¿ãï¼éãï¼
Android Studioããã¦ã³ãã¼ããã
Download Android Studio and SDK tools | Android DevelopersããAndroidStudioããã¦ã³ãã¼ããã¦ã¤ã³ã¹ãã¼ã«ããã
ãããã
ã¨ããããåºã¦ãã質åã«ã¯ãã¯ãã¨çãã¦ããã
ãã¦ã³ãã¼ãã¯ãã¾ãã
ãã£ã¡ãæéããã(^q^)
ããã¸ã§ã¯ããä½æãã
PJãæ°è¦ä½æããã
é©å½ã«çãã¦ã¿ãã
ãªããã§ããï¼ï¼ï¼
åããã¦ã¿ã
ã¨ãã¥ã¬ã¼ã¿ã¼ãåããã¦ã¿ãã¨ã¨ã©ã¼åºã
/Users/syobochim/Library/Android/sdk/tools/emulator -avd Android-L -netspeed full -netdelay none emulator: ERROR: This AVD's configuration is missing a kernel file!!
ããããªãã
ããã£ã¦ã¿ãããSDKが足りていないエラーã®ãããªã®ã§ããã¦ã³ãã¼ããã¦ã¿ãã
ãã¦ã³ãã¼ãããã£ã¡ãããã(ã»âã»)
ãã¦ã³ãã¼ãå®äºãã¦å度å®è¡ï¼ï¼
/Users/syobochim/Library/Android/sdk/tools/emulator -avd Android-L -netspeed full -netdelay none emulator: ERROR: This AVD's configuration is missing a kernel file!!
ãªãããªãâ¦ããã¬ããâ¦ã
ãªãå®æ©ã ï¼ï¼ï¼
Android Studioでアプリを実機で動作確認する - Android Studioでアプリ開発!ãè¦ãªããè¨å®ããã
Runï¼
ããããããï¼ï¼ï¼
Kotlin ç«¥è²ãã¦ãã§ï¼ï¼ï¼ï¼
Kotlinããããã¦éãããç´1å¹´åâ¦ï¼ã¤ãã«Kotlinç«¥è²ãæ¨ã¦ãã
Kotlinãããªããã¢ã¤ãã³ãã£ãã£ãªã®ã§ãæå¾ã¾ã§æ©ãã ãæ¨ã¦ããæ¨ã¦ããããï¼ï¼ï¼
ã¾ããããã©ã«ãã§ã§ããJava
package com.example.syobochim.syobochimapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Kotlinãã¡ã¤ã«ãä½æããã¨ããããã¢ãããåºãã
ã®ã§ãPluginãã¤ã³ã¹ãã¼ã«ããã
ããããã
åèµ·åããã¨ãããããã®ã§ã¦ããã
å¾ã£ã¦ã¿ãããbuild.gradleãå¤ãã£ãï¼ï¼ï¼ããããï¼ï¼
src/mainãã©ã«ãã®ä¸ã«kotlinãã£ã¬ã¯ããªãä½ãã¨ãsrcãã¹ããã§ã«éã£ã¦ããï¼ï¼
Kotlinãã¡ã¤ã«ãããã«ç§»åãããã
ã¨ããããããã©ã«ãã®MainActivityã¨åããã¨ãããã¦ã¿ããã®ã§ã
https://sites.google.com/site/tarokotlin/chap8/sec82ãè¦ãªããè¦ããè¦çä¼¼ã§æ¸ãã¦ã¿ãã
package com.example.syobochim.syobochimapp import android.app.Activity import android.os.Bundle import android.view.Menu import android.view.MenuItem class MainActivity() : Activity() { override fun onCreate(saveInstanceState: Bundle?) { super.onCreate(saveInstanceState) setContentView(R.layout.activity_main) } override fun onCreateOptionsMenu(menu : Menu): Boolean { getMenuInflater().inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.getItemId() if (id == R.id.action_settings) { return true } return super.onOptionsItemSelected(item) } }
ãã¨ãã¨ãã£ãJavaãã¡ã¤ã«ãåé¤ãã¦å®è¡ãã¦ã¿ãã
ãªãããã¾ããã£ãã£ã½ãï¼ï¼ï¼
ï¼ææ³ãããªãå²ã«ãããã£ã¨æ¸ãã¦ãã¾ãã®ã¤ã¤ï¼ï¼ï¼ï¼
ããã¦ãKotlinç«¥è²æ¨ã¦ããï¼ï¼ï¼ï¼ï¼ï¼ï¼
ã¨ããããæåã表示ãããã
æéããªãã®ã§ããããããã¨ãã¼ã¹ã§èª¿ã¹ã¦ããï¼ï¼
ãã¾ã®ã¨ããHello Worldãã表示ããã¦ããªãã®ã§ãã¨ãããã好ããªæåã表示ãããã
素人のアンドロイドアプリ開発日記「Hello world」を作成してみる。 - 素人のアンドロイドアプリ開発日記ãã¿ã¦ãæååãå¤ãã¦ã¿ãã
onCreateã®ä¸èº«ãå¤æ´ãããKotlinはインスタンス生成するときにnew書かなくてもいい。
override fun onCreate(saveInstanceState: Bundle?) { super.onCreate(saveInstanceState) val helloSyobochim = TextView(this) helloSyobochim.setText("syobochim app ã ãã¼ï¼ï¼ï¼") setContentView(helloSyobochim) }
ãã£ããã¼ï¼ï¼ï¼
ãã¿ã³ãã¤ãã¦ã¿ã
ãã¿ã³ãã¤ãã¦ã¿ãã
素人のアンドロイドアプリ開発日記アンドロイドアプリでクリックイベントを取得 - 素人のアンドロイドアプリ開発日記
ãµããµãã
activity_main.xmlã«ãã¿ã³ã追å ããã°ãããããªããã
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
ããããï¼ï¼ï¼ï¼ãªããã¨ã©ã¼ã§ã¦ç»é¢ã表示ãããªãâ¦ã
ããã£ããã©ããããªãå©ãã¦â¦ã
activity_main.xmlã«ã¨ããããããã¿ã³ã追å ãã¦ã¿ãâ¦ã
<Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button01" > </Button>
ã§ãKotlinã«ãå¦çãæ¸ãã¦ã¿ãã
変数はvarで宣言。
nullかもしれない場合は型に?をつける。
キャストする場合は「as 型」ï¼
OnClickListenerをImplementsする必要があった。Kotlinでは多重継承できる!
ãã ããsuperã¯ã©ã¹ãã©ã¡ãã®ã¯ã©ã¹ãæãã¦ããã®ãããããªããªã£ã¦ãã¾ãã®ã§ãã¸ã§ããªã¯ã¹ãã¤ãã¦ãã®ã¯ã©ã¹ãæãã¦ããã®ãæ確ã«ããå¿
è¦ãããã
class MainActivity() : Activity() , View.OnClickListener { var btn : Button? = null override fun onCreate(saveInstanceState: Bundle?) { super<Activity>.onCreate(saveInstanceState) setContentView(R.layout.activity_main) btn = findViewById(R.id.button01) as Button btn?.setText("ãªã©ãªã©ãªã©ï¼ï¼") btn?.setOnClickListener(this) } override fun onClick(v: View) { if (v == btn) { btn?.setText("ããããã¼ã¼ã¼ã¼ï¼ï¼ï¼ï¼") } }
ããããªãããããã«æååã¿ãã¦ããã©ã ããããã¶ãã¾ã ããã¦ãæéãããªãã
ãã¿ã³ãæ¼ããæã®åä½ã追å ãã
activity_main.xmlã§ã¬ã¤ã¢ã¦ããä½ããªããã¨ãæ²ããï¼æéããªãã®ã§ãå¥ããã¸ã§ã¯ããæ°è¦ä½æãã¦ãã¬ã¤ã¢ã¦ããä½ã£ã¦ãxmlã®ãªãã¿ã ãããããã¨ã«ããã
ããã¯ãããæ°ããã¦ããâ¦ï¼ï¼ï¼
Clickã¤ãã³ãã®æã«æåãåã£ã¦ãã¦è¿½å ï¼ã¯ä½è£ã§ã§ããï¼ï¼
package com.example.syobochim.syobochimapp import android.app.Activity import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.TextView import android.widget.Button import android.view.View import android.widget.EditText class MainActivity() : Activity(), View.OnClickListener { var btn: Button? = null var printText: TextView? = null var editText: EditText? = null var talk = StringBuilder("ããã¼ã¡ãã ãã¼ï¼") override fun onCreate(saveInstanceState: Bundle?) { super<Activity>.onCreate(saveInstanceState) setContentView(R.layout.activity_main) btn = findViewById(R.id.button01) as Button printText = findViewById(R.id.textView) as TextView editText = findViewById(R.id.editText01) as EditText btn?.setOnClickListener(this) } override fun onClick(v: View) { talk.append("\r\n") talk.append(editText?.getText()) printText?.setText(talk.toString()) editText?.setText("") } override fun onCreateOptionsMenu(menu: Menu): Boolean { getMenuInflater().inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.getItemId() if (id == R.id.action_settings) { return true } return super<Activity>.onOptionsItemSelected(item) } }
女ååbotã¨çµã¿åããããã£ã(失æ)
Clojureã§ä½ã£ã¦ã女子力botã¨çµã¿åããããã£ããã©ãæéåãã§ç¡çã ã£ãã
ããããã£ãã®ã¯ãããã¹ããå
¥åãã¦ããã¿ã³ãæ¼ãã¨ã女ååbotã®ããã«å¥³ååã®é«ã¾ã£ãæç« ãè¿ãã¦ããããã®ãä½ãããã£ããï¼ï¼ï¼
(ns jyoshiryoku-bot.core (:require [clojure.contrib.str-utils :as str-utils] [clojure.java.io :as io] [jyoshiryoku-bot.kaiseki :as kaiseki]) (:gen-class :name jyoshiryoku-bot.core :methods [#^{:static true} [talkSyobochim [String] String]])) (defn searchword [userInput] (kaiseki/token-word (first (kaiseki/tokenize userInput)))) (defn talkSyobochim [userInput] (kaiseki/init "tweet.txt") (str (kaiseki/create-sentence @kaiseki/*words* (searchword userInput)))) (defn -main [talk] (talkSyobochim talk)) (defn -talkSyobochim [userInput] (talkSyobochim userInput))
Clojureãjarãã¡ã¤ã«ã«åºãã¦ãããmain以å¤ã®é¢æ°ãå¼ã³åºãããããã¨ãé¢æ°ããå¤ãåå¾ãããããããããããªãã£ã(^q^)
調ã¹ã¦ãéã«æéåããããããClojureã¡ãããã ããã
ããã¦ããããªãã¨ãä½ããããªããï¼ï¼ï¼
ãã£ã¦ã¿ã¦
Kotlinããã£ã¡ãæ¸ããããã§ï¼ï¼ï¼ï¼
ãã£ã¡ãããã¼ãã®ããåºæ¥ãªãã£ãâ¦ã
ここã«ã²ã£ããç½®ã
ã¿ããªãKotlinããããã
ã¡ãªã¿ã«ã·ã¼ã«è²¼ã£ãã§ï¼ï¼ï¼