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();
}
});
}
}
반응형
'전공 공부 > 안드로이드프로그래밍' 카테고리의 다른 글
구글맵 (위성, 일반 지도) (0) | 2021.01.08 |
---|---|
DB Helper (0) | 2021.01.08 |
CheckBox, RadioButton (선택 항목 사진 출력) (0) | 2021.01.08 |
2개 액티비티 (그림 선택하면 액티비티에 확대되어 정보 출력) (0) | 2021.01.07 |
RadioButton, EditText (계산기) (0) | 2021.01.07 |