본문 바로가기

ANDROID

숫자 입력을 통한 연산

○ 두개의 숫자 입력 구간을 만들고 연산자 버튼을 통한 계산 결과를 나타냄

package com.example.mylabf;

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 Operator_0421 extends AppCompatActivity {

    private EditText editText,editText1;
    private TextView result;
    private int numresult;

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

        int [] btn_id = {R.id.btnadd,R.id.btnsub,R.id.btnmul,R.id.btndiv,R.id.btnrem,R.id.clearNumber};
        Button[] btn_ope = new Button[6];
        for(int i=0; i<6;i++){
            btn_ope[i] = findViewById(btn_id[i]);
        }

        editText = findViewById(R.id.inputNumber1);
        editText1 = findViewById(R.id.inputNumber2);
        result = findViewById(R.id.result);

        for (int i=0; i<6;i++){
            btn_ope[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button button = (Button)v;
                    String num1 = editText.getText().toString();
                    String num2 = editText1.getText().toString();

                    if (num1.equals("") || num2.equals("")){
                        result.setText("숫자를 입력하시오");
                    }
                   if(button.getText().toString().equals("+")){
                       if(!num1.equals("") && !num2.equals("")) {
                            numresult = Integer.parseInt(num1) + Integer.parseInt(num2);
                            result.setText(Integer.toString(numresult));
                        }
                    }
                   if(button.getText().toString().equals("-")){
                        if(!num1.equals("") && !num2.equals("")) {
                            numresult = Integer.parseInt(num1) - Integer.parseInt(num2);
                            result.setText(Integer.toString(numresult));
                        }
                   }
                   if(button.getText().toString().equals("*")) {
                       if (!num1.equals("") && !num2.equals("")) {
                           numresult = Integer.parseInt(num1) * Integer.parseInt(num2);
                           result.setText(Integer.toString(numresult));
                       }
                   }
                   if(button.getText().toString().equals("/")){
                       if(!num1.equals("") && !num2.equals("")) {
                           numresult = Integer.parseInt(num1) / Integer.parseInt(num2);
                           result.setText(Integer.toString(numresult));
                       }
                   }
                   if(button.getText().toString().equals("%")){
                       if(!num1.equals("") && !num2.equals("")) {
                           numresult = Integer.parseInt(num1) % Integer.parseInt(num2);
                           result.setText(Integer.toString(numresult));
                       }
                   }
                   if(button.getText().toString().equals("Clear")){
                       if(!num1.equals("") && !num2.equals("")) {
                           editText.setText("");
                           editText1.setText("");
                       }
                   }

                }
            });
        }
    }
}

<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=".Operator_0421">

    <EditText
        android:id="@+id/inputNumber1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="right"
        android:layout_margin="10dp"
        android:paddingTop="30dp"
        android:inputType="numberDecimal"
        android:hint="첫 번째 숫자" />

    <EditText
        android:id="@+id/inputNumber2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="right"
        android:layout_margin="10dp"
        android:paddingTop="10dp"
        android:inputType="numberDecimal"
        android:hint="두 번째 숫자" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="+" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="-" />

        <Button
            android:id="@+id/btnmul"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="*" />

        <Button
            android:id="@+id/btndiv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="/" />

        <Button
            android:id="@+id/btnrem"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="%" />
    </LinearLayout>

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="35dp"
        android:text="result" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/clearNumber"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="15dp"
            android:layout_margin="2dp"
            android:text="Clear" />
    </LinearLayout>

</LinearLayout>

<화면을 나타내는 xml파일>

 

     - 결과 화면

 

'ANDROID' 카테고리의 다른 글

숫자 버튼 만들기  (0) 2021.06.05
INTENT  (0) 2021.06.05
Layout2  (0) 2021.06.03
Layout1  (0) 2021.06.03
버튼 클릭  (0) 2021.06.03