0
0

More than 1 year has passed since last update.

App04 入れ子のLinearLayoutとToast

Posted at

App04 入れ子のLinearLayoutとToast

概要

LinearLayoutを入れ子にしたレイアウト作成と、トーストメッセージの表示方法を学習する。

画面

動作確認

ソースコード

カラー

colors.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#42A5F5</color>
    <color name="colorPrimaryDark">#1a73e8</color>
    <color name="colorAccent">#90CAF9</color>
</resources>

レイアウト

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="horizontal">

<Button
    android:id="@+id/btn1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:text="野菜"
    android:textSize="32sp"/>

<Button
android:id="@+id/btn2"
android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="肉"
    android:textSize="32sp" />

<Button
android:id="@+id/btn3"
android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="魚"
    android:textSize="32sp" />

    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
    android:text="米"
    android:textSize="32sp" />

<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
    android:text="パン"
    android:textSize="32sp" />

    </LinearLayout>

    </LinearLayout>


java

MainActivity.java

package com.example.app04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {   // 画面作成用クラス(Activity)を継承

    // クリック処理の中身だけを別途「clickListener」という名前の変数として用意。
    // 用意と同時に中身のonClick()の定義も行っている。
    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {    // 引数のview変数にはkクリックされたViewが渡される。
            Button btn = (Button)view;      // クリックされたViewをButtonクラスにキャスト(変換)
            // 画面下に表示される数秒のテキスト表示用処理
            // makeText()メソッドの引数には「Context、表示する文字、表示する長さ」を設定。ActivityはContextを継承しているので自身を渡している。
            // 最後にshow()メソッドで表示
            Toast.makeText(MainActivity.this, btn.getText().toString(),Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //各ボタンに対し、直接クリック処理を設定。
        findViewById(R.id.btn1).setOnClickListener(clickListener);  //btn1というIDのボタンに設定
        findViewById(R.id.btn2).setOnClickListener(clickListener);  //btn2というIDのボタンに設定
        findViewById(R.id.btn3).setOnClickListener(clickListener);  //btn3というIDのボタンに設定
        findViewById(R.id.btn4).setOnClickListener(clickListener);  //btn4というIDのボタンに設定
        findViewById(R.id.btn5).setOnClickListener(clickListener);  //btn5というIDのボタンに設定

    }

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0