2015年1月2日金曜日

ウィジェット上のtextの指定箇所

Androidアプリは、〈ボタン〉とかはオブジェクトじゃなくてウィジェット、って言うのかな?
んで、たとえば〈TextView〉とかに表示するテキストをプロパティの〈text〉で直接指定もできるんだけど、strings.xmlにまとめて書き留めて、こちらの情報を呼び出す方法もあるらしい。


・strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MyFirstApp05</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

</resources>


・activity_main.xml
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


strings.xmlで <string name="hello_world">Hello world!</string> と、指定したものを、 android:text="@string/hello_world" として呼び出している模様。

ボタンクリックでOKダイアログ



ボタンクリックでダイアログ表示成功。

・MainActivity.java

    // import android.view.View;

    public void showDialog(View v){

        AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
        alertDlg.setTitle("ダイアログタイトルです");
        alertDlg.setMessage("下のボタンクリックでこのダイアログを表示\n(゚∀゚)");
        alertDlg.setPositiveButton(
                "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // OK ボタンクリック処理
                    }
                });
        // 表示
        alertDlg.create().show();

    }



・activity_main.xml
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button01"
        android:onClick="showDialog"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="68dp" />

android:onClick="showDialog"
を追加。MainActivity.java内のメソッドを呼んでいる。



android:onClick="showDialog()"
と、メソッドの呼び出し時、メソッド名に「()」を付けて呼び出すとNG.



OKダイアログ



なんかダイアログを出す方法がいくつかあるっぽいけど、これが一番シンプルかな。

        // ダイアログテスト
        // import android.app.AlertDialog;
        // import android.content.DialogInterface;

        AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
        alertDlg.setTitle("ダイアログタイトルです");
        alertDlg.setMessage("メッセージです");
        alertDlg.setPositiveButton(
                "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // OK ボタンクリック処理
                    }
                });
        // 表示
        alertDlg.create().show();


http://androidguide.nomaki.jp/html/dlg/alert/alertdlgMain.html

ボタンクリックとか

http://so-zou.jp/mobile-app/tech/android/ui/event/listener.htm

へー、ボタンクリックの実装でもいろいろとあるのね。

<Button android:onClick="MethodName" />

こんな感じにLayout側でやるほうがすっきりしていいのかもね。