프로그래밍/안드로이드

안드로이드 - 1. 간단한 계산기 만들기

가카리 2017. 9. 17. 21:44
반응형
젤리빈 4.4 버전으로 작성되었습니다.

레이아웃은 단순히 입력을 받을 수 있는 EditText 상자 2개와

연산을 선택할 수 있는 버튼 4개

그리고 결과를 볼 수 있는 TextView 한개가 배치되어있습니다.




activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.kch.tutorial2.MainActivity">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
    >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
        >

            <EditText
                android:id="@+id/number1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:text="number1"
             />

            <EditText
                android:id="@+id/number2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:text="number2"
             />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp">

        <Button
            android:id="@+id/addbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add"
            android:onClick="addClick"
            />

        <Button
            android:id="@+id/subbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sub"
            tools:layout_editor_absoluteX="104dp"
            tools:layout_editor_absoluteY="120dp"
            android:onClick="subClick"
            />

        <Button
            android:id="@+id/mulbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mult"
            android:onClick="mulClick"
            />

        <Button
            android:id="@+id/divbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="div"
            android:onClick="divClick"
            />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="result"
                android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>


MainActivity.java

간단한 메소드이므로 따로 설명하지는 않습니다.

package com.example.kch.tutorial2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    //덧셈 버튼 클릭시
    public void addClick(View v){
        EditText number1 = (EditText)findViewById(R.id.number1);
        EditText number2 = (EditText)findViewById(R.id.number2);
        TextView result = (TextView)findViewById(R.id.result);

        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());

        result.setText(Integer.toString(n1+n2));

    }

    //뺄셈버튼 클릭시
    public void subClick(View v){
        EditText number1 = (EditText)findViewById(R.id.number1);
        EditText number2 = (EditText)findViewById(R.id.number2);
        TextView result = (TextView)findViewById(R.id.result);

        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());

        result.setText(Integer.toString(n1-n2));

    }

    //곱셈버튼 클릭시
    public void mulClick(View v){
        EditText number1 = (EditText)findViewById(R.id.number1);
        EditText number2 = (EditText)findViewById(R.id.number2);
        TextView result = (TextView)findViewById(R.id.result);

        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());

        result.setText(Integer.toString(n1*n2));

    }

    //나눗셈 버튼클릭시
    public void divClick(View v){
        EditText number1 = (EditText)findViewById(R.id.number1);
        EditText number2 = (EditText)findViewById(R.id.number2);
        TextView result = (TextView)findViewById(R.id.result);

        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());

        result.setText(Integer.toString(n1/n2));

    }

}