본문 바로가기

Android

사칙연산 화면 만들기

Android


OS(Operation System:운영체제:Window,Rinux)

OS(Operation System:안드로이드:안드로이드(구글),IOS)

 

activity_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"
    >
    <EditText
        android:id="@+id/edtNum1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:hint="숫자입력"
        android:inputType="number"

        />
    <EditText
        android:id="@+id/edtNum2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:hint="숫자입력"
        android:inputType="number"

        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10sp"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="더하기"
            android:textSize="20sp"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="곱하기"
            android:textSize="20sp"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="나누기"
            android:textSize="20sp"
            />
        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="빼기"
            android:textSize="20sp"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="결과"
        android:textSize="30sp" />
</LinearLayout>

MainActivity.java

package com.example.ex02;

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;

public class MainActivity extends AppCompatActivity {

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


        final EditText edtNum1=findViewById(R.id.edtNum1);
        final EditText edtNum2=findViewById(R.id.edtNum2);
        final TextView txtResult=findViewById(R.id.txtResult);



        Button btn1=findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 int intNum1=Integer.parseInt(edtNum1.getText().toString());
                 int intNum2=Integer.parseInt(edtNum2.getText().toString());
                int intResult=intNum1+intNum2;
                txtResult.setText("더한 결과는"+intResult+"입니다.");
            }
        });
        Button btn2=findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int intNum1=Integer.parseInt(edtNum1.getText().toString());
                int intNum2=Integer.parseInt(edtNum2.getText().toString());
                int intResult=intNum1*intNum2;
                txtResult.setText("곱한 결과는" +intResult+"입니다.");
            }
        });
        Button btn3=findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int intNum1=Integer.parseInt(edtNum1.getText().toString());
                int intNum2=Integer.parseInt(edtNum2.getText().toString());
                double douResult=intNum1/(double)intNum2;
                txtResult.setText("나눈결과는"+douResult+"입니다.");
            }
        });
        Button btn4=findViewById(R.id.btn4);
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int intNum1=Integer.parseInt(edtNum1.getText().toString());
                int intNum2=Integer.parseInt(edtNum2.getText().toString());
                int intResult=intNum1-intNum2;
                txtResult.setText("뺀결과는" +intResult+"입니다.");
            }
        });

    }
}

'Android' 카테고리의 다른 글

캘린더를 활용한 다이어리 앱 만들기  (0) 2019.11.11
주소록만들기(버튼,옵션메뉴)  (0) 2019.11.07
사칙연산 화면 만들기  (0) 2019.11.07
메뉴와 서브메뉴만들기  (0) 2019.11.07
ListView,삭제버튼,입력버튼  (0) 2019.11.06