艦これメモ+開発メモ+その他若干

ゲーム「艦隊これくしょん」の自分用メモとそれに基づいた簡単な記事、開発関連の自分用メモ、旅行時のメモなど

GUIでXMLを更新してAndroidのレイアウトを組むのではなく完全手動でやってみた

純度100%のサーバ屋が手慰みにAndroidStudioを少しいじってみて、「これGUIXML更新してレイアウト組むより手動で組んだ方が楽じゃね?」って思って試しにやってみたコードのメモ。
多分、手でAndroidJavaのコード書いてる人にとっては常識なんだろうと思うけど、習熟段階でのメモなので、まぁ許してください。

入力フィールドとボタンがあって、ボタンを押したら「入力した値」をダイアログ上に表示し、ダイアログ上のOKボタンを押すと入力フィールドをクリアして戻る。というだけの簡単なアプリケーションを、レイアウトのXMLやRオブジェクトを使わず書いてみた。

package com.example.xxxxxxxx.myapplication2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    /** タイトルのラベル */
    protected TextView textView = null;

    /** テキスト入力ボックス */
    protected EditText editText = null;

    /** 実行ボタン */
    protected Button button = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createContext();
    }

    private void createContext() {
        // 多分AWTのContentPaneみたいな。
        final MainActivity activity = this;

        // 一番外側に FrameLayout を作る。
        FrameLayout frameLayout = new FrameLayout(this);
        this.setContentView(frameLayout);

        // FrameLayoutの要素として、縦方向のLinearLayout を作る。
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        frameLayout.addView(linearLayout);

        // TextView(JLabelみたいなの)を作ってLinearLayoutに食わせる
        textView = new TextView(this);
        textView.setText("Comment:");
        textView.setTextSize(36.0f);
        linearLayout.addView(textView);

        // EditText(JTextFieldみたいなの)を作ってLinearLayoutに食わせる
        editText = new EditText(this);
        editText.setTextSize(24.0f);
        linearLayout.addView(editText);

        // Button(JButtonみたいなの)を作ってLinearLayoutに食わせる
        button = new Button(this);
        button.setText("submit");
        button.setTextSize(32.0f);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.button_onClick(view);
            }
        });
        linearLayout.addView(button);
    }

    public void button_onClick(View view) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Result");
        dialog.setMessage(editText.getText().toString());
        dialog.setPositiveButton(
            "OK", 
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  editText.setText("");
              }
        });
        dialog.show();
    }

}

FrameLayout要らなかったかも。
根っこがサーバ屋でGUIもSwingやSWTで少し書く程度のクライアント素人なので、ツッコミどころ多数な気配はしますが、見逃してください。

やてみると、この方が楽だし自分にとっては理解しやすいんだけど、「すでにできる事」をやっているだけで勉強にはならないかなぁ。
あと、「自力でできるからこれでいーや」ってやっちゃうのは良くないっすね。
そういう癖がついちゃうと 、Erlangの癖が全く抜けていないElixirのコード書く人とか、C++なのにクラスを構造体としてしか使っていないロートルC技術者みたいな事になりそうなので、GUIのツールとXMLとRオブジェクトでも表現できる場合は、できるだけGUIのツールとXMLとRオブジェクトを使うよう心がけておこうと思いますた。