독도갈매기의 개발 블로그

[안드로이드] 계산기 만들기 -1- Layout 본문

안드로이드

[안드로이드] 계산기 만들기 -1- Layout

독도갈매기 2020. 10. 4. 14:19

오늘은 학교에서 나온 안드로이드 과제 계산기를 만들어보겠습니다.
저는 안드로이드를 정말로 극혐좋아하기 때문에^^ 관련 포스팅이 많이 답답할수도 있다는 점을 알려드립니다. (_._)

주어진 레이아웃 제작하기

img

추석기간동안의 과제로 내주셨는데요 ㅎㅎ 추석에는 쉬게해주시지...
레이아웃이 조금... 그래도 일단 만들어봤습니다.

mycalc

위 화면이 실제로 나오는 레이아웃입니다.
안드로이드는 자신있게 설명할 수 있는 분야가 아니라고 생각되어서 주석으로 설명드리는 점 죄송합니다 ㅎㅎ;;

<?xml version="1.0" encoding="utf-8"?>
<!--    주어진 레이아웃에 맞게 기본으로 세로 정렬(vertical)을 해줍니다.-->
<LinearLayout 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"
    android:orientation="vertical">

<!--    이름을 표시할 텍스트 뷰를 생성합니다.-->
    <TextView
        android:id="@+id/opcheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="간단한 계산기 앱 00000 홍길동"
        android:gravity="center"
        android:textColor="#000000"/>

<!--    입력값을 표시할 텍스트 뷰를 생성합니다.-->
    <TextView
        android:id="@+id/inputBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="버튼을 눌러 값을 입력해주세요..."
        android:textSize="20dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        />

<!--    결과값을 표실할 텍스트 뷰 또한 생성합니다.-->
    <TextView
        android:id="@+id/resultBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="결과값"
        android:textColor="#000000"
        android:textSize="20dp"
        android:gravity="center"
        />

<!--    기본 정렬을 세로 정렬(vertical)했기 때문에 가로(horizontal)로 정렬하기 위한 LinearLayout을 생성합니다.-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

<!--     layout_weight로 균등하게 비율을 나눠줍니다.-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_weight="1"
            android:onClick="pushOneNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1"
            android:onClick="pushTwoNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="1"
            android:onClick="pushThreeNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1"
            android:onClick="pushFourNum"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="1"
            android:onClick="pushFiveNum"
            />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="1"
            android:onClick="pushSixNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="1"
            android:onClick="pushSevenNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:layout_weight="1"
            android:onClick="pushEightNum"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:layout_weight="1"
            android:onClick="pushNineNum"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1"
            android:onClick="pushZeroNum"
            />
    </LinearLayout>

<!--    사칙연산을 할 수 있게 버튼을 구현합니다.-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="더하기"
        android:onClick="plusNumber"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="빼기"
        android:onClick="minusNumber"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="곱하기"
        android:onClick="multiNum"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나누기"
        android:onClick="divideNumber"/>
</LinearLayout>

위 코드에서 onClick에 사용되는 메소드들의 자세한 코드는 다음편인 기능구현에서 설명하겠습니다.

TextView에 값을 추가하는 함수를 각 숫자 버튼마다 할당을 해주고,
그 값을 사칙연산 버튼을 누르면 처음 받았던 값과 두 번째 받은 값을 눌렀던 사칙연산을 실행하게 하도록 구현 할 예정입니다.
많이 부족한 안드로이드 포스팅도 읽어주셔서 감사합니다!

'안드로이드' 카테고리의 다른 글

[안드로이드] 계산기 만들기 -2- 기능 구현  (0) 2020.10.05
Comments