반응형
MainActivity.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 |
package com.ktds.myactivityforresult;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText one;
private EditText two;
private Button plus;
private Button minus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText) findViewById(R.id.one);
two = (EditText) findViewById(R.id.two);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String oneValue = one.getText().toString();
String twoValue = two.getText().toString();
Intent intent = new Intent(MainActivity.this, calcActivity.class);
intent.putExtra("one",oneValue);
intent.putExtra("two",twoValue);
intent.putExtra("requestCode",1001);
startActivityForResult(intent, 1001);
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String oneValue = one.getText().toString();
String twoValue = two.getText().toString();
Intent intent = new Intent(MainActivity.this, calcActivity.class);
intent.putExtra("one",oneValue);
intent.putExtra("two",twoValue);
intent.putExtra("requestCode",1002);
startActivityForResult(intent, 1002);
}
});
}
/**
* Activity로 부터 응답을 받으면 실행되는 메소드
* @param requestCode : 요청코드
* @param resultCode : 결과코드
* @param data : resultIntent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1001 || requestCode==1002){
if(resultCode==RESULT_OK){
String result = data.getIntExtra("result",0)+"";
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
}
}
|
cs |
calcActivity.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 |
package com.ktds.myactivityforresult;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class calcActivity extends ActionBarActivity {
private Button btnCalc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
btnCalc = (Button) findViewById(R.id.btnCalc);
Intent intent = getIntent();
final String one = intent.getStringExtra("one");
final String two = intent.getStringExtra("two");
final int requestCode = intent.getIntExtra("requestCode",0);
btnCalc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = 0;
if(requestCode==1001) {
result = Integer.parseInt(one) + Integer.parseInt(two);
}
else if(requestCode==1002){
result = Integer.parseInt(one) - Integer.parseInt(two);
}
Intent resultIntent = new Intent();
resultIntent.putExtra("result", result);
// setResult(응답코드, 정보를 전달 할 intent)
setResult(RESULT_OK, resultIntent);
finish();
}
});
}
}
|
cs |
activity_calc.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.ktds.myactivityforresult.calcActivity">
<Button
android:id="@+id/btnCalc"
android:text="계산하기"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
|
cs |
activity_main.xml
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 |
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/plus"
android:text="더하기"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/minus"
android:text="빼기"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
|
cs |
반응형
'IT > Android' 카테고리의 다른 글
2. Activity Life Cycle(Activity 수명 주기) (0) | 2015.06.22 |
---|---|
1. Activity Life Cycle(Activity 수명 주기) 및 Log 남기기 (0) | 2015.06.22 |
Layout (0) | 2015.06.22 |
ListView (0) | 2015.06.18 |
Container - TabHost (0) | 2015.06.18 |