728x90

# 실행 화면

# activity1.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=".Activity1">

    <EditText
        android:id="@+id/editTextNumber1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/editTextNumber2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />

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

        <RadioButton
            android:id="@+id/radio_add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="더하기" />

        <RadioButton
            android:id="@+id/radio_sub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="빼기" />

        <RadioButton
            android:id="@+id/radio_mul"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="곱하기" />

        <RadioButton
            android:id="@+id/radio_div"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="나누기" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="계산하기" />

</LinearLayout>

# activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="돌아가기"/>
</LinearLayout>

# Activity1.java

package kr.ac.skuniv.intenttest2;

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 Activity1 extends AppCompatActivity {
    EditText eText1, eText2;
    Button button;
    RadioGroup rGroup;
    RadioButton rButton;
    static final int GET_STRING = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
        eText1 = (EditText) findViewById(R.id.editTextNumber1);
        eText2 = (EditText) findViewById(R.id.editTextNumber2);
        eText1.setInputType(0);
        eText2.setInputType(0);

        rGroup = (RadioGroup) findViewById(R.id.radioGroup);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rButton = (RadioButton) findViewById(rGroup.getCheckedRadioButtonId());

                Intent intent = new Intent(Activity1.this, Activity2.class);
                intent.putExtra("num1", Integer.parseInt(eText1.getText().toString()));
                intent.putExtra("num2", Integer.parseInt(eText2.getText().toString()));
                intent.putExtra("op", rButton.getText().toString());

                startActivityForResult(intent,GET_STRING);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GET_STRING) {
            if (resultCode == RESULT_OK)
                Toast.makeText(getApplication(), data.getStringExtra("result"), Toast.LENGTH_SHORT).show();
        }
    }
}

# Activity2.java

package kr.ac.skuniv.intenttest2;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends AppCompatActivity {
    Button button;
    int result = 0;
    @Override
    protected void onCreate(Bundle savedInstancedState){
        super.onCreate(savedInstancedState);
        setContentView(R.layout.activity2);
        button = (Button) findViewById(R.id.back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                int num1 = intent.getIntExtra("num1", 0);
                int num2 = intent.getIntExtra("num2",0);
                String op = intent.getStringExtra("op");

                if(op.equals("더하기"))
                    result = num1 + num2;
                else if(op.equals("빼기"))
                    result = num1 - num2;
                else if(op.equals("곱하기"))
                    result = num1 * num2;
                else
                    result = num1 / num2;

                System.out.println(result);
                intent.putExtra("result", "결과 " + result);
                setResult(RESULT_OK, intent);
                finish();
            }
         });
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기