Android [ Java, Kotlin ]

모바일 프로그래밍 과제(Java) - 중첩 레이아웃

Moonsu99 2023. 10. 14. 19:03

환경

  • OS - Mac OS 13.5.2
  • Tools - Android Studio Iguana | 2023.2.1 Canary 5
  • Language - Java
  • android version - 12
  • tartgetSDK - 33
  • minSDK - 28

조건

1. View는 6개의 버튼으로 구성된 화면이 표시된다.

2.  색상과 배치가 그림과 동일하도록 레이아웃을 구성해야 된다.

3. 버튼을 누르면 그 버튼이 사라지고 인접한 버튼들이 그 공간을 차지한다. 

4. 버튼을 모두 클릭하여 사라지면 “버튼이 모두 사라졌습니다라는 토스트 메시지를 나타내야 한다.

 

 

구현 코드

 

activity_main.xml

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

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

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FF0000"
            android:text="1"
            android:textSize="30sp"
            android:textColor="@color/black"/>

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ABF200"
            android:text="2"
            android:textSize="30sp"
            android:textColor="@color/black"/>

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0054FF"
            android:text="3"
            android:textSize="30sp"
            android:textColor="@color/black"/>
    </LinearLayout>

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

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

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#FFE400"
                android:text="4"
                android:textSize="30sp"
                android:textColor="@color/black"/>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#00D8FF"
                android:text="5"
                android:textSize="30sp"
                android:textColor="@color/black"/>
        </LinearLayout>

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

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#BDBDBD"
                android:text="6"
                android:textSize="30sp"
                android:textColor="@color/black"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

 

Mainactivity.java

package com.example.knu_mobile2;

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 implements View.OnClickListener {

    private int buttonCount = 6; // 버튼의 총 개수

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

        // 버튼들을 찾아와서 클릭 리스너를 설정.
        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);
        Button button4 = findViewById(R.id.button4);
        Button button5 = findViewById(R.id.button5);
        Button button6 = findViewById(R.id.button6);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // 클릭된 버튼을 숨깁니다.
        view.setVisibility(View.GONE);
        buttonCount--;

        // 모든 버튼이 사라졌을 때 토스트 메시지를 표시합니다.
        if (buttonCount == 0) {
            Toast.makeText(this, "버튼이 모두 사라졌습니다", Toast.LENGTH_SHORT).show();
        }
    }
}