Android, IOS/Android

[Android Sutdio를 활용한 안드로이드 프로그래밍] 연습문제 5-6

Emil :) 2019. 10. 9. 15:38
728x90
반응형

<문제>

다음 화면을 프로젝트로 완성하시오. 리니어레이아웃을 이용해서 XML코드로 만들고, 각 레이아웃을 클릭하면 레이아웃의 폭과 높이가 토스트 메시지로 출력되도록 Java 프로그래밍을 한다.(안쪽 레이아웃은 모두 정사각형이며, 크기는 안쪽 레이아웃부터 50dp, 150dp, 250dp 이다. 각 레이아웃의 색상은 서로 다르게 한다.)

결과 화면

 

<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:background="#df0ef9"
    android:id="@+id/purple"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/blue"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="#0000ff">

        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/yellow"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="#f0ff00">

            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/black"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:background="#000000">
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<java 코드>

방법이 두가지가 있다. 클릭 리스너를 무명클래스로 작성하는 방법과, 리스너를 클래스로 정의하는 방법이 있다.
(무명클래스로 작성하는 방법이 쉬운데, 교수님이 코드를 줄이기 위해 사용하는 방법을 알려주셔서 써먹어본다.)

<방법 1, 무명클래스>

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    LinearLayout purple, blue, yellow, black;
    private int width, height;

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

        purple = findViewById(R.id.purple);
        blue = findViewById(R.id.blue);
        yellow = findViewById(R.id.yellow);
        black = findViewById(R.id.black);

		purple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = purple.getWidth();
                height = purple.getHeight();
                Toast.makeText(getApplicationContext(), "가로 : " + width + ", 세로 : " + height, Toast.LENGTH_SHORT).show();
            }
        });

        blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = blue.getWidth();
                height = blue.getHeight();
                Toast.makeText(getApplicationContext(), "가로 : " + width + ", 세로 : " + height, Toast.LENGTH_SHORT).show();
            }
        });

        yellow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = yellow.getWidth();
                height = yellow.getHeight();
                Toast.makeText(getApplicationContext(), "가로 : " + width + ", 세로 : " + height, Toast.LENGTH_SHORT).show();
            }
        });

        black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = black.getWidth();
                height = black.getHeight();
                Toast.makeText(getApplicationContext(), "가로 : " + width + ", 세로 : " + height, Toast.LENGTH_SHORT).show();
            }
        });
        
    }
}

<방법 2, 내부 클래스 정의>

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    LinearLayout purple, blue, yellow, black;
    private int width, height;

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

        purple = findViewById(R.id.purple);
        blue = findViewById(R.id.blue);
        yellow = findViewById(R.id.yellow);
        black = findViewById(R.id.black);

        MyListener listener = new MyListener(); //리스너 객체 생성
        purple.setOnClickListener(listener);
        blue.setOnClickListener(listener);
        yellow.setOnClickListener(listener);
        black.setOnClickListener(listener);
    }

    public class MyListener implements View.OnClickListener {
        @Override
        public void onClick(View v){
            width = 0;
            height = 0;
            switch(v.getId()){
                case R.id.purple:
                    width = purple.getWidth();
                    height = purple.getHeight();
                    break;
                case R.id.blue:
                    width = blue.getWidth();
                    height = blue.getHeight();
                    break;
                case R.id.yellow:
                    width = yellow.getWidth();
                    height = yellow.getHeight();
                    break;
                case R.id.black:
                    width = black.getWidth();
                    height = black.getHeight();
                    break;
            }
            Toast.makeText(getApplicationContext(), "가로 : " + width + ", 세로 : " + height, Toast.LENGTH_SHORT).show();
        }
    }
}

 

<결과>

 

..근데 올리고보니 길이가 비슷하다 ㅇㅅㅇ..
나중에 큰 프로젝트 할때는 내부 클래스 정의로 하는게 좋을듯

728x90
반응형