728x90
한빛아카데미 Android Studio를 활용한 안드로이드 프로그래밍 6판
p.412 직접 풀어보기 10-3
두 수를 입력하고 더하기, 빼기, 곱하기, 나누기 라디오버튼을 선택한 후 <계산하기>를 클릭하면 세컨드 엑티비티에서 계산하고 값을 돌려받는 앱을 작성하라.
코드
MainActivity.java
package com.cookandroid.test;
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.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 엑티비티");
final EditText edtNum1 = findViewById(R.id.edtNum1);
final EditText edtNum2 = findViewById(R.id.edtNum2);
final RadioGroup rdoGroup = findViewById(R.id.rdoGroup);
Button btnNewActivity = findViewById(R.id.btnNewActivity);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
switch (rdoGroup.getCheckedRadioButtonId()) {
case R.id.rdoAdd:
intent.putExtra("Calc", "+");
break;
case R.id.rdoSub:
intent.putExtra("Calc", "-");
break;
case R.id.rdoMul:
intent.putExtra("Calc", "*");
break;
case R.id.rdoDiv:
intent.putExtra("Calc", "/");
break;
default:
break;
}
intent.putExtra("Num1",
Integer.parseInt(edtNum1.getText().toString()));
intent.putExtra("Num2",
Integer.parseInt(edtNum2.getText().toString()));
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
int hap = data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "결과 :" + hap,
Toast.LENGTH_SHORT).show();
}
}
}
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" />
<RadioGroup
android:id="@+id/rdoGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rdoAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="더하기" />
<RadioButton
android:id="@+id/rdoSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="빼기" />
<RadioButton
android:id="@+id/rdoMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="곱하기" />
<RadioButton
android:id="@+id/rdoDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나누기" />
</RadioGroup>
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="계산하기" />
</LinearLayout>
SecondActivity.java
package com.cookandroid.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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.second);
setTitle("Second 액티비티");
Intent inIntent = getIntent();
String calc = (inIntent.getStringExtra("Calc"));
int calValue = 0;
if (calc.equals("+")) {
calValue = inIntent.getIntExtra("Num1", 0) + inIntent.getIntExtra("Num2", 0);
} else if (calc.equals("-")) {
calValue = inIntent.getIntExtra("Num1", 0) - inIntent.getIntExtra("Num2", 0);
} else if (calc.equals("*")) {
calValue = inIntent.getIntExtra("Num1", 0) * inIntent.getIntExtra("Num2", 0);
} else {
calValue = inIntent.getIntExtra("Num1", 0) / inIntent.getIntExtra("Num2", 0);
}
final int retValue = calValue;
Button btnReturn = findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
outIntent.putExtra("Hap", retValue);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
결과
728x90
'개발 > 책' 카테고리의 다른 글
[안드로이드 프로그래밍] 11장 직접 풀어보기 11-1 (0) | 2021.12.07 |
---|---|
[명품 웹 프로그래밍] 9장 Open Challenge (0) | 2021.11.22 |
[안드로이드 프로그래밍] 10장 직접 풀어보기 10-2 (0) | 2021.11.20 |