JavaでAndroid Studioアプリ開発を試みるの第3回 「strings.xml」

4255ecad936e4cd1e7822b7cd9d7c05e 1

未だに、画面構成しかできておりません。
Android Studioのアプリ開発の続きを頑張りたいと思います。

 

目次

まずは前回の復習から

一旦前回のおさらい。
赤文字の箇所が開いているスペース分だけサイズを広げましょうっていう部分です。
そして、青文字の部分がボタンを右側においてますよの記載です。

まぁ、でも細かいことは気にしなくてもとりあえずは見える位置に配置できていれば良いでしょう。
ここでチェックしておきたいのは1つ。
編集テキスト、ボタン、テキストに@+id/***の***の部分で名前を付けているってことですね。
これは自分好みの名前を付けると良いでしょう。

res -> layout -> activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/inputText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:text="@string/input_text_default"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/confirmButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <Button
        android:id="@+id/confirmButton"
        android:text="@string/confirm_btn_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/outputText"
        android:text="@string/output_text_default"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputText"
        android:textSize="24sp"
        />


    </androidx.constraintlayout.widget.ConstraintLayout>

 

 

文字列をまとめたstrings.xml

もう一つ忘れてました。
ボタンや編集ウィンドウに初期表示する文字です。

中身としては下記のとおりです。
最初から存在しているのが”app_name”というやつです。
他の部分(赤文字)は自分で追加しました。

res -> values -> strings.xml

<resources>
    <string name="app_name">メモ帳アプリ</string>
    <string name="confirm_btn_name">追加</string>
    <string name="input_text_default"></string>
    <string name="output_text_default">上枠にメモを書いてください。</string>
</resources>

 

まず、先ほどのactivity_main.xmlで設定したのが各部品のIDです。

入力用のテキストエリアのID:inputText
メモ追加用のボタンのID:confirmButton
出力のテキストエリアのID:outputText

そこで、直接表示する文字などを設定してもいいのですが、Androidアプリでは別のファイルで表示する文字は一括で管理するのが推奨されています。
そのリストが上記の「strings.xml」です。

入力用のテキストエリアのID:inputTextでいうと、そこに初期値として表示される文字は「android:text=”@string/input_text_default”」の部分です。
ここの””の間に文字を直接書いても正しく表示されます。
しかし、推奨はされない。

代わりに@string/input_text_defaultというような@string/***という形式で記載します。
上記でいうと「input_text_default」を指定していて、それの中身がなんだろうというと、先ほどの「strings.xml」に記載された内容です。
まぁ、結果的には上記の例では何も記載していないので、初期値は何も表示されません。
こうしておくことで、あとで初期値を追加したいというときには簡単に編集できます。

 

ちょっと難しいリスナ

さて、実際に書いていくと、中々道のりは長い。。
次のステップとしてはボタンを押したら何らかの反応をするようにしたいと思います。

このボタンでちょっと理解が難しいのは、リスナという概念でしょう。
普通にボタンを用意して、そのボタンが押された時のボタンの処理を書きます。
そして、サンプルプログラムを動作させてみる。

そうすると、ボタンを押しても何も起こらない。。

こうなる訳です、これの理由はボタンが押されたことをチェックする部分が記載がないからです。
このボタンが押されないかチェックする部分を記載しなくてはならないのです。

これの具体的な手順を今回書こうと思ったのですが、その手前の説明でちょっと消耗してしまったので、続きは次回で。。

 

Android Studioを使用したアプリ開発を試みるの第2回 「画面構成」
https://aoi345.com/memorandum/android-gamen/

あわせて読みたい
JavaでAndroid Studioアプリ開発を試みるの第2回 「Layout」 さて、Androidアプリの開発メモの続きです。 勢いで行けるところまで行きたいと思います。 何しろ1か月でメモ帳アプリを完成させる。 ただし、その完成度は規定しない。...

 

よかったらシェアしてね!
  • URLをコピーしました!
目次