파일명 : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edtNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="계산하기"/>
<RadioGroup
android:id="@+id/rGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rBtnSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기" />
<RadioButton
android:id="@+id/rBtnMinus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="빼기" />
<RadioButton
android:id="@+id/rBtnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="곱하기" />
<RadioButton
android:id="@+id/rBtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나누기" />
</RadioGroup>
</LinearLayout>
파일명 : activity_second.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"
android:orientation="vertical">
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
</LinearLayout>
파일명 : MainActivity.java
package com.example.it.myapplication8_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");
Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
final RadioGroup rGroup = (RadioGroup) findViewById(R.id.rGroup);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edtNum1 = (EditText) findViewById(R.id.edtNum1);
EditText edtNum2 = (EditText) findViewById(R.id.edtNum2);
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
switch (rGroup.getCheckedRadioButtonId())
{
case R.id.rBtnSum:
intent.putExtra("Operator", "+");
break;
case R.id.rBtnMinus:
intent.putExtra("Operator", "-");
break;
case R.id.rBtnMul:
intent.putExtra("Operator", "*");
break;
case R.id.rBtnDiv:
intent.putExtra("Operator", "/");
break;
}
intent.putExtra("Num1", Integer.parseInt(edtNum1.getText().toString()));
intent.putExtra("Num2", Integer.parseInt(edtNum2.getText().toString()));
//startActivity();// 주고 끝낸다.
//startActivityForResult(); //결과 응답을 기다린다(받을때까지)
startActivityForResult(intent, 0);
//1. request: 요청
//2. response: 응답 == result:결과
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
int calResult = data.getIntExtra("CalResult", 0);
RadioGroup rGroup = (RadioGroup) findViewById(R.id.rGroup);
String str = "";
switch (rGroup.getCheckedRadioButtonId()) {
case R.id.rBtnSum :
str = "더하기 결과";
break;
case R.id.rBtnMinus :
str = "빼기 결과";
break;
case R.id.rBtnMul :
str = "곱하기 결과";
break;
case R.id.rBtnDiv :
str = "나누기 결과";
break;
}
Toast.makeText(getApplicationContext(), str + " : " + calResult, Toast.LENGTH_SHORT).show();
}
}
}
파일명 : SecondActivity.java
package com.example.it.myapplication8_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("Second 액티비티");
Intent inIntent = getIntent();
String operator = inIntent.getStringExtra("Operator");
final int num1 = inIntent.getIntExtra("Num1", 0);
final int num2 = inIntent.getIntExtra("Num2", 0);
int HapValue = 0;
if(operator.equals("+")) {
HapValue = num1 + num2;
} else if(operator.equals("-")) {
HapValue = num1 - num2;
} else if(operator.equals("*")) {
HapValue = num1 * num2;
} else if(operator.equals("/")) {
HapValue = num1 / num2;
}
Button btnReturn = (Button) findViewById(R.id.btnReturn);
final int finalHapValue = HapValue;
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
outIntent.putExtra("CalResult", finalHapValue);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}
파일명 : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it.myapplication8_7">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
'IT > Android' 카테고리의 다른 글
android - 액티비티 생명주기 (0) | 2018.09.25 |
---|---|
android - 암시적 인텐트 (0) | 2018.09.25 |
android - 양방향 intent (0) | 2018.09.24 |
android - 투표 최대결과 인텐트 전환하며 값 넘겨주기 (Activity, Intent, putExtra, getIntent, getIntArrayExtra, getStringArrayExtra) (0) | 2018.09.23 |
android - ratingBar (0) | 2018.09.23 |