본문 바로가기

카테고리 없음

Android 계산기 만들기

결과값 :




activity_main.xml


<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"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp" >
    <TextView
        android:id="@+id/View"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:text="계산 결과 :"
        android:textSize="30dp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/Edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:hint="첫 번째 숫자" />

    <EditText
        android:id="@+id/Edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:hint="두 번째 숫자" />

    <Button
        android:id="@+id/BtnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#D592B7"
        android:text="더하기" />

    <Button
        android:id="@+id/BtnHy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#CF10D2"
        android:text="빼기" />

    <Button
        android:id="@+id/BtnGob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#5BA917"
        android:text="곱하기" />

    <Button
        android:id="@+id/BtnMod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#8A7E98"
        android:text="나누기" />
</LinearLayout> 



MainActivity.java


package com.example.android_calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    // 개체 접근 변수 선언
    Button button1; // 더하기
    Button button2; // 빼기
    Button button3; // 곱하기
    Button button4; // 나누기

    EditText t1; // 첫번쨰 입력하기
    EditText t2; // 두번쨰 입력하기

    // 연산하기 위한 변수 선언
    TextView tv; // 총결과값넣어주기
    int tx1, tx2, result, a;

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

        t1 = (EditText) findViewById(R.id.Edit1);
        t2 = (EditText) findViewById(R.id.Edit2);

        tv = (TextView) findViewById(R.id.View);

        button1 = (Button) findViewById(R.id.BtnAdd);
        button2 = (Button) findViewById(R.id.BtnHy);
        button3 = (Button) findViewById(R.id.BtnGob);
        button4 = (Button) findViewById(R.id.BtnMod);

        // 나누기
        button4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calc(0);
            }
        });

        // 곱하기
        button3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calc(1);
            }
        });

        // 빼기
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calc(2);

            }
        });

        // 더하기
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calc(3);
            }
        });
    }

    private void calc(int a) {

        switch (a) {
        case 0:
            result = Integer.parseInt(t1.getText().toString())
                    / Integer.parseInt(t2.getText().toString());
            break;
        case 1:
            result = Integer.parseInt(t1.getText().toString())
                    * Integer.parseInt(t2.getText().toString());
            break;
        case 2:
            result = Integer.parseInt(t1.getText().toString())
                    - Integer.parseInt(t2.getText().toString());
            break;
        case 3:
            result = Integer.parseInt(t1.getText().toString())
                    + Integer.parseInt(t2.getText().toString());
            break;
        }
        tv.setText("계산결과값 : " + result);
    }