본문 바로가기

동방프로젝트

직풀 10-3

package com.example.forseveralactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import org.w3c.dom.Text;

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("세컨드 액티비티");

//메인이 넘긴 인텐트를 getIntent()로 받고, 이를 따로 저장
Intent intent=getIntent();
String calc=(intent.getStringExtra("Calc"));

int calValue=0;
if(calc.equals("+")) {
calValue=intent.getIntExtra("Num1", 0) +
intent.getIntExtra("Num2", 0);
}
else if(calc.equals("-")) {
calValue=intent.getIntExtra("Num1", 0)-
intent.getIntExtra("Num2", 0);
}
else if(calc.equals("*")) {
calValue=intent.getIntExtra("Num1", 0)*
intent.getIntExtra("Num2", 0);
}
else if(calc.equals("/")) {
calValue=intent.getIntExtra("Num1", 0) /
intent.getIntExtra("Num2", 0);
}

final int retValue=calValue;

Button btnReturn=(Button)findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//보낼 인텐트를 생성, 여기에 putExtra를 통해 값 넣음
//이걸 setResult를 통해 보냄.
Intent outIntent=new Intent(getApplicationContext(),
MainActivity.class);
outIntent.putExtra("Hap", retValue);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}

//////////////////

package com.example.forseveralactivity;

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.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

//직풀: 투표결과, 화면에서 가장 많은 표를 받은 그림과 제목을 보여줌
//result.xml(second.xml) ResultActivity.java만을 수정
//이미지파일의 id를 저장할 배열을 만들고, 레이팅바를 작게 변경
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");

final EditText edtNum1 = (EditText) findViewById(R.id.edtNum1);
final EditText edtNum2 = (EditText) findViewById(R.id.edtNum2);
final RadioGroup rdoGroup = (RadioGroup) findViewById(R.id.rdoGroup);

Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);

btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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;
}

//새 액티비티를 화면에 출력하려면 startActivity를 쓴다
//이 메소드는 파라미터를 인텐드받는다. 인텐트를 생성하고 두 값을 넣음
//값을 다시 돌려받기 위해 startActivityForResult를 사용
//두번째 파라미터엔 돌려받을 값이 있을 경우에 0 이상을 써준다.
intent.putExtra("Num1", Integer.parseInt(
edtNum1.getText().toString()));
intent.putExtra("Num2", Integer.parseInt(
edtNum2.getText().toString()));
}
});
}
@Override
protected void onActivityResult(int requestCode, int ResultCode,
Intent data) {
super.onActivityResult(requestCode, ResultCode, data);
if (ResultCode==RESULT_OK) {
int hap=data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "결과: "+hap,
Toast.LENGTH_SHORT).show();
}
}
}

//////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"
android:id="@+id/btnReturn"/>


</LinearLayout>

////////////////////////

<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtNum1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtNum2"/>

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

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:check="true"
android:text="더하기"
android:id="@+id/rdoAdd"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="곱하기"
android:id="@+id/rdoMul"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="빼기"
android:id="@+id/rdoSub"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나누기"
android:id="@+id/rdoDiv"/>
</RadioGroup>

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

</LinearLayout>

//////////////////

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forseveralactivity">

<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"
android:label="Second 액티비티" />
<activity android:name=".ThirdActivity"
android:label="Third 액티비티"/>
</application>

</manifest>

'동방프로젝트' 카테고리의 다른 글

리스트뷰  (2) 2020.08.19
암시적 인텐트  (2) 2020.08.19
실습 10-2, 직풀 10-2  (0) 2020.08.18
직풀 10-1  (0) 2020.08.17
개선그림판 9-3  (2) 2020.08.17