2010年4月19日月曜日

Clojure on AndroidでAlertDialog

Xperiaを買ったのでClojure on Androidで遊ぼうとしている。

JavaもClojureもAndroidも素人なのでいろんなとこで時間をくってるが、とりあえずボタンクリックでAlertDialogを表示するところまでいった。

(ns org.example.Test.AlertDialog
(:gen-class
:extends android.app.Activity
:implements (android.view.View$OnClickListener)
:exposes-methods {onCreate superOnCreate}))

(import '(android.app AlertDialog)
'(android.util Log))

(defn -onCreate [this #^android.os.Bundle bundle]
(.superOnCreate this bundle)
(.setContentView this org.example.Test.R$layout/main)
(let [btn (.findViewById this org.example.Test.R$id/Btn1)]
(Log/d "tag" (str "this is log:" (.toString btn)))
(.setOnClickListener btn this)))

(defn -onClick [this view]
(Log/d "tag" "this is log2:" view)
(let [al (new android.app.AlertDialog$Builder this)]
(doto al
(.setTitle "AlertDialog!")
(.setMessage "hoge!")
(.setCancelable true))
(.. al create show)))

Btn1はres/layout/main.xml中でandroid:id="@+id/Btn1"と定義したボタン。

ログの出力はandroid.util.Logクラスのstaticメソッドで行える。

ネストクラス(クラスの中で定義されたクラスなど)は/ではなく$で参照するらしい。

悪い例) android.app.AlertDialog.Builder
悪い例) android.app.AlertDialog/Builder
良い例) android.app.AlertDialog$Builder