728x90
반응형

리니어 레이아웃으로 계산기 만들고 이벤트 설정하기

 

xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/edText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="더하기"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="빼기"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="곱하기"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="나누기"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center|left"
        android:text="계산 결과 : "
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp" />
</LinearLayout>

 

 

Java

package com.example.a2c_1005_4;

import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText edText1, edText2;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView text1;
    String num1,num2;
    Integer result;

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

        setTitle("미니 계산기");

        edText1 = findViewById(R.id.edText1);
        edText2 = findViewById(R.id.edText2);

        btnAdd = findViewById(R.id.btnAdd);
        btnSub = findViewById(R.id.btnSub);
        btnMul = findViewById(R.id.btnMul);
        btnDiv = findViewById(R.id.btnDiv);

        text1 = findViewById(R.id.text1);



        btnAdd.setOnTouchListener(new View.OnTouchListener(){
            @SuppressLint("SetTextI18n")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edText1.getText().toString();
                num2 = edText2.getText().toString();

                // 계산 결과 정수형 더하기
                result = Integer.parseInt(num1) + Integer.parseInt(num2);
                text1.setText("계산결과 : " + result.toString());      // 계산 결과


                return false;
            }
        });


        btnSub.setOnTouchListener(new View.OnTouchListener(){
            @SuppressLint("SetTextI18n")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edText1.getText().toString();
                num2 = edText2.getText().toString();

                // 계산 결과 빼기
                result = Integer.parseInt(num1) - Integer.parseInt(num2);
                text1.setText("계산결과 : " + result.toString());

                return false;
            }
        });

        btnMul.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edText1.getText().toString();
                num2 = edText2.getText().toString();

                // 계산 결과 곱하기
                result = Integer.parseInt(num1) * Integer.parseInt(num2);
                text1.setText("계산결과 : " + result.toString());

                return false;
            }
        });

        btnDiv.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                num1 = edText1.getText().toString();
                num2 = edText2.getText().toString();

                // 계산 결과 나누기
                result = Integer.parseInt(num1) / Integer.parseInt(num2);
                text1.setText("계산결과 : " + result.toString());

                return false;
            }
        });


    }
}

 

 

더하기 결과 

 

[그림1] 더하기 결과

빼기 결과 

 

[그림2] 빼기 결과

 

곱하기 결과

 

[그림3] 곱하기 결과

 

 

나누기 결과

 

[그림4] 나누기 결과

728x90
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기