Programming/Android

안드로이드 프로그래밍 4장 초간단계산기

fishersheep 2021. 8. 16. 17:37
반응형

main.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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="30dp">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_margin="10dp"
        android:text="숫자1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:ems="10"
        android:inputType="number"
        android:text="숫자2" />

    <Button
        android:id="@+id/button1"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="더하기" />

    <Button
        android:id="@+id/button2"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="빼기" />

    <Button
        android:id="@+id/button3"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="곱하기" />

    <Button
        android:id="@+id/button4"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나누기" />

    <Button
        android:id="@+id/button5"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나머지값" />




    <TextView
        android:id="@+id/TextResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textColor="#FF0000"
        android:layout_margin="10dp"
        android:text="계산결과: " />
</LinearLayout>

mainActivity.java

package com.cookandroid.project4_1;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    EditText edit1, edit2;
    Button btnadd,btnsub,btnmul,btndiv,btntemp;
    TextView textresult;
    String num1,num2;
    Integer result;


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

        setTitle("초간단계산기");

        edit1 = (EditText) findViewById(R.id.editText1);
        edit2 = (EditText) findViewById(R.id.editText2);
        btnadd = (Button) findViewById(R.id.button1);
        textresult = (TextView) findViewById(R.id.TextResult);

        btnsub = (Button)findViewById(R.id.button2);
        btnmul = (Button)findViewById(R.id.button3);
        btndiv = (Button)findViewById(R.id.button4);
        btntemp = (Button)findViewById(R.id.button5);

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();

                if(edit1.getText().toString().length() == 0 || edit2.getText().toString().length() == 0)
                {
                    Toast.makeText(getApplicationContext(), "값을 입력하세요.", Toast.LENGTH_SHORT).show();
                    textresult.setText("계산결과: none" );
                }
                else {
                    result = Integer.parseInt(num1) + Integer.parseInt(num2);
                    textresult.setText("계산결과: " + result.toString());
                }
            }
        });

        btnsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                if(edit1.getText().toString().length() == 0 || edit2.getText().toString().length() == 0)
                {
                    Toast.makeText(getApplicationContext(), "값을 입력하세요.", Toast.LENGTH_SHORT).show();
                    textresult.setText("계산결과: none" );
                }
                else {
                    result = Integer.parseInt(num1) - Integer.parseInt(num2);
                    textresult.setText("계산결과: " + result.toString());
                }
            }
        });

        btnmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                if(edit1.getText().toString().length() == 0 || edit2.getText().toString().length() == 0)
                {
                    Toast.makeText(getApplicationContext(), "값을 입력하세요.", Toast.LENGTH_SHORT).show();
                    textresult.setText("계산결과: none" );
                }
                else {
                    result = Integer.parseInt(num1) * Integer.parseInt(num2);
                    textresult.setText("계산결과: " + result.toString());
                }
            }
        });

        btndiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                if(edit1.getText().toString().length() == 0 || edit2.getText().toString().length() == 0 ||Integer.parseInt(num2)==0)
                {
                    Toast.makeText(getApplicationContext(), "계산할수없습니다.", Toast.LENGTH_SHORT).show();
                    textresult.setText("계산결과: none" );
                }
                else {
                    result = Integer.parseInt(num1) / Integer.parseInt(num2);
                    textresult.setText("계산결과: " + result.toString());
                }
            }
        });

        btntemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                if(edit1.getText().toString().length() == 0 || edit2.getText().toString().length() == 0)
                {
                    Toast.makeText(getApplicationContext(), "값을 입력하세요.", Toast.LENGTH_SHORT).show();
                    textresult.setText("계산결과: none" );
                }
                else {
                    result = Integer.parseInt(num1) % Integer.parseInt(num2);
                    textresult.setText("계산결과: " + result.toString());
                }
            }
        });


    }
}

초간단계산기

반응형