반응형
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 |
package com.ktds.mycalculator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private EditText edtResult;
private Button btnOne, btnTwo, btnThree, btnFore, btnFive, btnSix, btnSeven, btnEight, btnNine, btnZero, btnDoubleZero;
private Button btnResult, btnMinus, btnPlus, btnMul, btnDiv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtResult = (EditText) findViewById(R.id.edtResult);
btnOne = (Button) findViewById(R.id.btnOne);
btnTwo = (Button) findViewById(R.id.btnTwo);
btnThree = (Button) findViewById(R.id.btnThree);
btnFore = (Button) findViewById(R.id.btnFore);
btnFive = (Button) findViewById(R.id.btnFive);
btnSix = (Button) findViewById(R.id.btnSix);
btnSeven = (Button) findViewById(R.id.btnSeven);
btnEight = (Button) findViewById(R.id.btnEight);
btnNine = (Button) findViewById(R.id.btnNine);
btnZero = (Button) findViewById(R.id.btnZero);
btnDoubleZero = (Button) findViewById(R.id.btnDoubleZero);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMul = (Button) findViewById(R.id.btnMul);
btnDiv = (Button) findViewById(R.id.btnDiv);
btnResult = (Button) findViewById(R.id.btnResult);
btnOne.setOnClickListener(new MyButtornClickListener());
btnTwo.setOnClickListener(new MyButtornClickListener());
btnThree.setOnClickListener(new MyButtornClickListener());
btnFore.setOnClickListener(new MyButtornClickListener());
btnFive.setOnClickListener(new MyButtornClickListener());
btnSix.setOnClickListener(new MyButtornClickListener());
btnSeven.setOnClickListener(new MyButtornClickListener());
btnEight.setOnClickListener(new MyButtornClickListener());
btnNine.setOnClickListener(new MyButtornClickListener());
btnZero.setOnClickListener(new MyButtornClickListener());
btnDoubleZero.setOnClickListener(new MyButtornClickListener());
btnMinus.setOnClickListener(new MyButtornClickListener());
btnPlus.setOnClickListener(new MyButtornClickListener());
btnMul.setOnClickListener(new MyButtornClickListener());
btnDiv.setOnClickListener(new MyButtornClickListener());
btnResult.setOnClickListener(new MyButtornClickListener());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/*
onPause 직전에 실행되며, 초기화 되는 데이터를 보존하기 위해 outState에 저장한다.
*/
outState.putInt("tempNumber",tempNumber);
outState.putString("oper", oper);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
/*
onResum 직전에 실행되며, Bundle의 데이터를 꺼내온다.
*/
tempNumber = savedInstanceState.getInt("tempNumber");
oper = savedInstanceState.getString("oper");
}
// private static를 사용하면 위의 메소드를 사용하지 않아도 된다.
// 하지만, 이 변수 말고도 다른 변수들이 많으면 메모리 관리상 위의 메소드를 활용하는것이 좋다.
String oper = "";
int tempNumber = 0;
private class MyButtornClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
int id = v.getId();
String numbers = edtResult.getText().toString();
if(numbers.equals("0")){
numbers = "";
}
else if(id == R.id.btnZero){
edtResult.setText(numbers+"0");
}
else if(id == R.id.btnDoubleZero){
edtResult.setText(numbers+"00");
}
else if(id == R.id.btnOne){
edtResult.setText(numbers+"1");
}
else if(id == R.id.btnTwo){
edtResult.setText(numbers+"2");
}
else if(id == R.id.btnThree){
edtResult.setText(numbers+"3");
}
else if(id == R.id.btnFore){
edtResult.setText(numbers+"4");
}
else if(id == R.id.btnFive){
edtResult.setText(numbers+"5");
}
else if(id == R.id.btnSix){
edtResult.setText(numbers+"6");
}
else if(id == R.id.btnSeven){
edtResult.setText(numbers+"7");
}
else if(id == R.id.btnEight) {
edtResult.setText(numbers + "8");
}
else if(id == R.id.btnNine) {
edtResult.setText(numbers + "9");
}
if(id == R.id.btnPlus){
tempNumber = Integer.parseInt(numbers);
edtResult.setText("0");
oper = "+";
}
else if(id == R.id.btnMinus){
tempNumber = Integer.parseInt(numbers);
edtResult.setText("0");
oper = "-";
}
else if(id == R.id.btnMul){
tempNumber = Integer.parseInt(numbers);
edtResult.setText("0");
oper = "*";
}
else if(id == R.id.btnDiv){
tempNumber = Integer.parseInt(numbers);
edtResult.setText("0");
oper = "/";
}
if(id==R.id.btnResult){
int tempNumber2 = Integer.parseInt(numbers);
int result = 0;
if(oper.equals("+")){
result = tempNumber+tempNumber2;
}
else if(oper.equals("-")){
result = tempNumber-tempNumber2;
}
else if(oper.equals("*")){
result = tempNumber*tempNumber2;
}
else if(oper.equals("/")){
result = tempNumber/tempNumber2;
}
edtResult.setText(result+"");
}
}
}
}
|
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 |
<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/edtResult"
android:hint="숫자 입력"
android:layout_gravity="fill_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnSeven"
android:text="7"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnEight"
android:text="8"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnNine"
android:text="9"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnDiv"
android:text="/"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnFore"
android:text="4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnFive"
android:text="5"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnSix"
android:text="6"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnMul"
android:text="*"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnThree"
android:text="3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnTwo"
android:text="2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnOne"
android:text="1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnPlus"
android:text="+"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnZero"
android:text="0"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnDoubleZero"
android:text="00"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnResult"
android:text="="
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnMinus"
android:text="-"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
|
cs |
반응형
'IT > Android' 카테고리의 다른 글
Database (0) | 2015.06.23 |
---|---|
Receiver (0) | 2015.06.23 |
2. Activity Life Cycle(Activity 수명 주기) (0) | 2015.06.22 |
1. Activity Life Cycle(Activity 수명 주기) 및 Log 남기기 (0) | 2015.06.22 |
1. Activity를 이용한 계산기 (0) | 2015.06.22 |