개발/책

[안드로이드 프로그래밍] 5장 직접 풀어보기 5-5

Hyunsun 2021. 10. 6. 00:00
728x90
한빛아카데미 Android Studio를 활용한 안드로이드 프로그래밍 6판

 

p.232 직접 풀어보기 5-5

 

[실습 5-2]를 그리드레이아웃으로 변경하여 실행하라.

HINT 그리드레이아웃은 위젯의 높이 조절이 잘 되지 않아 전체 화면이 채워지지 않을 수 있다.

HINT Java 코드는 고칠 필요가 없고 XML만 변경하면 된다. XML 위젯의 id도 동일하게 사용한다.

 

코드

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="5"
    android:rowCount="9">

    <EditText
        android:id="@+id/Edit1"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:hint="숫자1입력" />

    <EditText
        android:id="@+id/Edit2"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:hint="숫자1입력" />

    <Button
        android:id="@+id/BtnNum0"
        android:layout_column="0"
        android:layout_row="2"
        android:text="0" />

    <Button
        android:id="@+id/BtnNum1"
        android:layout_column="1"
        android:layout_row="2"
        android:text="1" />

    <Button
        android:id="@+id/BtnNum2"
        android:layout_column="2"
        android:layout_row="2"
        android:text="2" />

    <Button
        android:id="@+id/BtnNum3"
        android:layout_column="3"
        android:layout_row="2"
        android:text="3" />

    <Button
        android:id="@+id/BtnNum4"
        android:layout_column="4"
        android:layout_row="2"
        android:text="4" />

    <Button
        android:id="@+id/BtnNum5"
        android:layout_column="0"
        android:layout_row="3"
        android:text="5" />

    <Button
        android:id="@+id/BtnNum6"
        android:layout_column="1"
        android:layout_row="3"
        android:text="6" />

    <Button
        android:id="@+id/BtnNum7"
        android:layout_column="2"
        android:layout_row="3"
        android:text="7" />

    <Button
        android:id="@+id/BtnNum8"
        android:layout_column="3"
        android:layout_row="3"
        android:text="8" />

    <Button
        android:id="@+id/BtnNum9"
        android:layout_column="4"
        android:layout_row="3"
        android:text="9" />

    <Button
        android:id="@+id/BtnAdd"
        android:layout_margin="5dp"
        android:layout_column="0"
        android:layout_row="4"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:text="더하기" />

    <Button
        android:id="@+id/BtnSub"
        android:layout_margin="5dp"
        android:layout_column="0"
        android:layout_row="5"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:text="빼기" />

    <Button
        android:id="@+id/BtnMul"
        android:layout_margin="5dp"
        android:layout_column="0"
        android:layout_row="6"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:text="곱하기" />

    <Button
        android:id="@+id/BtnDiv"
        android:layout_margin="5dp"
        android:layout_column="0"
        android:layout_row="7"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:text="나누기" />

    <TextView
        android:id="@+id/TextResult"
        android:layout_margin="5dp"
        android:layout_column="0"
        android:layout_row="8"
        android:layout_columnSpan="5"
        android:layout_gravity="fill_horizontal"
        android:text="계산결과: "
        android:textColor="#FF0000"
        android:textSize="20sp" />

</GridLayout>

 

MainActivity.java

package com.cookandroid.test;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText edit1, edit2;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView textResult;
    String num1, num2;
    Button[] numButtons = new Button[10];
    int[] numBtnIDs = {R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4,
            R.id.BtnNum5, R.id.BtnNum6, R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("테이블레이아웃 계산기");
        edit1 = findViewById(R.id.Edit1);
        edit2 = findViewById(R.id.Edit2);
        btnAdd = findViewById(R.id.BtnAdd);
        btnSub = findViewById(R.id.BtnSub);
        btnMul = findViewById(R.id.BtnMul);
        btnDiv = findViewById(R.id.BtnDiv);
        textResult = findViewById(R.id.TextResult);
        btnAdd.setOnClickListener(this);
        btnSub.setOnClickListener(this);
        btnMul.setOnClickListener(this);
        btnDiv.setOnClickListener(this);

        for (int i = 0; i < numBtnIDs.length; i++) {
            numButtons[i] = findViewById(numBtnIDs[i]);
        }

        for (int i = 0; i < numBtnIDs.length; i++) {
            final int index = i;
            numButtons[index].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (edit1.isFocused() == true) {
                        num1 = edit1.getText().toString()
                                + numButtons[index].getText().toString();
                        edit1.setText(num1);
                    } else if (edit2.isFocused() == true) {
                        num2 = edit2.getText().toString()
                                + numButtons[index].getText().toString();
                        edit2.setText(num2);
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "먼저 에디트텍스트를 선택하세요", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        num1 = edit1.getText().toString();
        num2 = edit2.getText().toString();
        int result = 0;
        if (num1 == null || num2 == null) {
            Toast.makeText(getApplicationContext(), "입력", Toast.LENGTH_SHORT).show();
        }
        switch (view.getId()) {
            case R.id.BtnAdd:
                result = Integer.parseInt(num1) + Integer.parseInt(num2);
                break;
            case R.id.BtnSub:
                result = Integer.parseInt(num1) - Integer.parseInt(num2);
                break;
            case R.id.BtnMul:
                result = Integer.parseInt(num1) * Integer.parseInt(num2);
                break;
            case R.id.BtnDiv:
                result = Integer.parseInt(num1) / Integer.parseInt(num2);
                break;
        }
        textResult.setText("계산 결과 : " + result);
    }
}

 

결과

728x90