PROGRAMMING

안드로이드 실습 - 연산 어플 본문

Android

안드로이드 실습 - 연산 어플

Raccoon2125 2020. 10. 27. 00:07

1. MainActivity.java

package com.example.tutorial1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View; // 뷰 객체
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); // 메인 액티비티의 레이아웃을 따서 뷰 생성
    }

    public void addClick(View v)
    {
        EditText number1 = (EditText) findViewById(R.id.number1);
        EditText number2 = (EditText) findViewById(R.id.number2);
        TextView result = (TextView) findViewById(R.id.result);
        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());
        result.setText(Integer.toString(n1 + n2));
    }

    public void subtractClick(View v)
    {
        EditText number1 = (EditText) findViewById(R.id.number1);
        EditText number2 = (EditText) findViewById(R.id.number2);
        TextView result = (TextView) findViewById(R.id.result);
        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());
        result.setText(Integer.toString(n1 - n2));
    }

    public void multipleClick(View v)
    {
        EditText number1 = (EditText) findViewById(R.id.number1);
        EditText number2 = (EditText) findViewById(R.id.number2);
        TextView result = (TextView) findViewById(R.id.result);
        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());
        result.setText(Integer.toString(n1 * n2));
    }

    public void divideClick(View v)
    {
        EditText number1 = (EditText) findViewById(R.id.number1);
        EditText number2 = (EditText) findViewById(R.id.number2);
        TextView result = (TextView) findViewById(R.id.result);
        int n1 = Integer.parseInt(number1.getText().toString());
        int n2 = Integer.parseInt(number2.getText().toString());
        result.setText(Integer.toString(n1 / n2));
    }
}

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <EditText
        android:id="@+id/number1"
        android:layout_width="409dp" <!-- match_parent, wrap_content 사용하지 않고 수동 설정 -->
        android:layout_height="48dp"
        android:ems="10"
        android:hint="첫 번째 숫자"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="70dp" />

    <EditText
        android:id="@+id/number2"
        android:layout_width="411dp"
        android:layout_height="46dp"
        android:ems="10"
        android:hint="두 번째 숫자"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="122dp" />

    <Button
        android:id="@+id/subtractButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="빼기"
        tools:layout_editor_absoluteX="109dp"
        tools:layout_editor_absoluteY="168dp"
        android:onClick="subtractClick"/>

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="더하기"
        tools:layout_editor_absoluteX="7dp"
        tools:layout_editor_absoluteY="168dp"
        android:onClick="addClick"/>

    <Button
        android:id="@+id/multiplyButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="곱하기"
        tools:layout_editor_absoluteX="209dp"
        tools:layout_editor_absoluteY="168dp"
        android:onClick="multipleClick"/>

    <Button
        android:id="@+id/divideButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="나누기"
        tools:layout_editor_absoluteX="309dp"
        tools:layout_editor_absoluteY="168dp"
        android:onClick="divideClick"/>

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        tools:layout_editor_absoluteX="9dp"
        tools:layout_editor_absoluteY="219dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
Comments