관리 메뉴

FU11M00N

[Android] 액티비티와 인텐트를 이용한 사칙연산. 본문

Programming/Android

[Android] 액티비티와 인텐트를 이용한 사칙연산.

호IT 2020. 11. 2. 17:17

이번 글은 따로 설명이 없는 코드글입니다.

문제

위의 문제 풀기!

package org.techtown.a2b_20201102_2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edtNum1, edtNum2;
    RadioGroup rdoGroup1;
    RadioButton rdoAdd,rdoSub,rdoMul,rdoDiv;
    Button btnCalc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtNum1 = findViewById(R.id.edtNum1);
        edtNum2 = findViewById(R.id.edtNum2);
        rdoGroup1 = findViewById(R.id.rdoGroup1);
        rdoAdd = findViewById(R.id.rdoAdd);
        rdoSub = findViewById(R.id.rdoSub);
        rdoMul = findViewById(R.id.rdoMul);
        rdoDiv = findViewById(R.id.rdoDiv);
        btnCalc = findViewById(R.id.btnCalc);


        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                int num1 = Integer.parseInt(edtNum1.getText().toString());
                int num2 = Integer.parseInt(edtNum2.getText().toString());

                intent.putExtra("num1",num1);
                intent.putExtra("num2",num2);



                switch (rdoGroup1.getCheckedRadioButtonId()){
                    case R.id.rdoAdd:
                        intent.putExtra("operator","+");
                        break;
                    case R.id.rdoSub:
                        intent.putExtra("operator","-");
                        break;
                    case R.id.rdoMul:
                        intent.putExtra("operator","*");
                        break;
                    case R.id.rdoDiv:
                        intent.putExtra("operator","/");
                }
                startActivityForResult(intent,0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK){
            Toast.makeText(getApplicationContext(),"결과 :"
                    +data.getIntExtra("result",0),Toast.LENGTH_LONG).show();
        }
    }
}

MainActivity.java

<?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/edtNum1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <EditText
        android:id="@+id/edtNum2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <RadioGroup
        android:id="@+id/rdoGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/rdoAdd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="덧셈"
            android:textSize="30sp" />

        <RadioButton
            android:id="@+id/rdoSub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="뺄셈"
            android:textSize="30sp" />

        <RadioButton
            android:id="@+id/rdoMul"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="곱셈"
            android:textSize="30sp" />

        <RadioButton
            android:id="@+id/rdoDiv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="나눗셈"
            android:textSize="30sp" />

    </RadioGroup>

    <Button
        android:id="@+id/btnCalc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="계산하기"
        android:textSize="36sp" />
</LinearLayout>

activity_main.xml

 

package org.techtown.a2b_20201102_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class SecondActivity extends AppCompatActivity {
    Button btnFinish;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btnFinish = findViewById(R.id.btnFinish);

        Intent intent = getIntent();

        final int num1= intent.getIntExtra("num1",0);
        final int num2= intent.getIntExtra("num2",0);
        final String operator = intent.getStringExtra("operator");

        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentResult = new Intent(getApplicationContext(),MainActivity.class);
                if(operator.equals("+")){
                    intentResult.putExtra("result",num1+num2);
                }
                if(operator.equals("-")){
                    intentResult.putExtra("result",num1-num2);
                }
                if(operator.equals("*")){
                    intentResult.putExtra("result",num1*num2);
                }
                if(operator.equals("/")){
                    intentResult.putExtra("result",num1/num2);
                }

                setResult(RESULT_OK,intentResult);
                finish();
            }
        });
    }
}

SecondActivity.java

 

<?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=".SecondActivity">

    <Button
        android:id="@+id/btnFinish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="돌아가기" />
</LinearLayout>

activity_second.xml

 

더하기 결괏 값.

나머지 결괏 값 생략....

Comments