Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 보안뉴스요약
- 자바스크립트 prototype
- 보안뉴스 요약
- 깃허브
- 카카오프로젝트
- 자바스크립트
- 자바스크립트 node
- 다크웹
- 자바스크립트 element api
- Oracle SQL
- php
- 보안뉴스
- 자바스크립트 객체
- oracle db
- GIT
- 보안뉴스한줄요약
- 카카오프로젝트 100
- python
- 자바스크립트 API
- 파이썬
- javascript
- oracle
- 오라클
- ES6
- 랜섬웨어
- 자바스크립트 jQuery
- 자바스크립트 기본 문법
- 보안뉴스 한줄요약
- 카카오프로젝트100
- numpy
Archives
- Today
- Total
FU11M00N
[Android] 액티비티와 인텐트를 이용한 사칙연산. 본문
이번 글은 따로 설명이 없는 코드글입니다.
위의 문제 풀기!
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
나머지 결괏 값 생략....
'Programming > Android' 카테고리의 다른 글
[Android] 세 개 이상의 화면 만들어 전환하기. (0) | 2020.11.16 |
---|---|
[Android] intent와 rating bar를 이용한 명화 투표 기능 (0) | 2020.11.10 |
[Andriod] Activity의 onCreateOptionsMenu() 오버라이딩 (0) | 2020.11.02 |
[안드로이드] 안드로이드 스튜디오(java)로 계산기 디자인, 기능 구현 (0) | 2020.10.12 |
안드로이드 스튜디오 설치하기. (0) | 2020.09.01 |
Comments