๊ธฐ๋ณธ์ ์ธ ์์ ฏ์ธ ํ ์คํธ๋ทฐ, ์๋ํธํ ์คํธ, ๋ฒํผ์ ์ด์ฉํ์ฌ ์ ์ ๊ณ์ฐ๊ธฐ ์ฑ ๋ง๋ค์ด ๋ณด๊ธฐ
XML ์ฝ๋(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
|
<?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=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="์ซ์1" />
<EditText
android:id="@+id/Edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="์ซ์2" />
<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋ํ๊ธฐ"/>
<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋นผ๊ธฐ"/>
<Button
android:id="@+id/BtnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๊ณฑํ๊ธฐ"/>
<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋๋๊ธฐ"/>
<TextView
android:id="@+id/TextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#FF0000"
android:layout_margin="10dp"
android:text="๊ณ์ฐ ๊ฒฐ๊ณผ : " />
</LinearLayout>
|
cs |
android:id๋ก ์๋ฐ ์ฝ๋์์ ๋ถ๋ฌ์ฌ ์ ์๊ฒ id๋ฅผ ์ ํด์ฃผ๊ณ
android:layout_width์android:layout_height์ผ๋ก ์์ ฏ์ ํฌ๊ธฐ(๋๋น์ ๋์ด)๋ฅผ ์ง์ ํด ์ค๋ค.
android:textSize๋ก ๊ธ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํด์ฃผ๊ณ
android:textColor ํ ์คํธ ๊ธ์ ์์ ์ ํด์ค๋ค. ๊ฒฐ๊ณผ ๊ฐ์ด ์ ๋ณด์ด๊ฒ๋ RGB ๊ฐ์ค ๋นจ๊ฐ์์ผ๋ก ์ ํด์ฃผ์๋ค.
android:layout_margin="10dp"์ผ๋ก ์์ ฏ๊ฐ์ ๊ฐ๊ฒฉ์ ์กฐ์ ํด์ฃผ์๋ค.
android:text์ผ๋ก ์์ ฏ ์์ ํ ์คํธ ๋ด์ฉ์ ์ ํด์ฃผ์๋ค.
<LineanrLayout>์์ android:orientation์ vertical๋ก ์ฃผ์ด ์์ ฏ๋ค์ด ์ธ๋ก๋ก ๋์ด๋๊ฒ ํ๋ค.
![](https://blog.kakaocdn.net/dn/cOf5Ix/btrPYDGNaWw/o0bl0VgsKD9uvNvQ8pIa4K/img.png)
![](https://blog.kakaocdn.net/dn/Rx1yS/btrPTjCwL8T/0eugkIH2cb9ynUDTEnWNeK/img.png)
![](https://blog.kakaocdn.net/dn/d6zMi3/btrPU0WQFjT/kTcKekDWAktaemMQiX0JVk/img.png)
Java ์ฝ๋(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
|
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit1, edit2;
Button btnAdd, btnSub, btnMul, btnDiv;
TextView textResult;
String num1, num2;
Integer result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("์ด๊ฐ๋จ ์ ์ ๊ณ์ฐ๊ธฐ");
edit1=(EditText) findViewById(R.id.Edit1);
edit2=(EditText) findViewById(R.id.Edit2);
btnAdd = (Button) findViewById(R.id.BtnAdd);
btnSub = (Button) findViewById(R.id.BtnSub);
btnMul = (Button) findViewById(R.id.BtnMul);
btnDiv = (Button) findViewById(R.id.BtnDiv);
textResult = (TextView) findViewById(R.id.TextResult);
btnAdd.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
num1= edit1.getText().toString();
num2= edit2.getText().toString();
result= Integer.parseInt(num1) + Integer.parseInt(num2);
textResult.setText("๊ณ์ฐ ๊ฒฐ๊ณผ : "+ result.toString());
return false;
}
});
btnSub.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
num1= edit1.getText().toString();
num2= edit2.getText().toString();
result= Integer.parseInt(num1) - Integer.parseInt(num2);
textResult.setText("๊ณ์ฐ ๊ฒฐ๊ณผ : "+ result.toString());
return false;
}
});
btnMul.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
num1= edit1.getText().toString();
num2= edit2.getText().toString();
result= Integer.parseInt(num1) * Integer.parseInt(num2);
textResult.setText("๊ณ์ฐ ๊ฒฐ๊ณผ : "+ result.toString());
return false;
}
});
btnDiv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
num1= edit1.getText().toString();
num2= edit2.getText().toString();
result= Integer.parseInt(num1) / Integer.parseInt(num2);
textResult.setText("๊ณ์ฐ ๊ฒฐ๊ณผ : "+ result.toString());
return false;
}
});
}
}
|
cs |
13์ด~17์ด
activity_main.xml์ 7๊ฐ ์์ ฏ์ ๋์ํ ์์ ฏ ๋ณ์ 7๊ฐ, ์ ๋ ฅ๋ 2๊ฐ ๋ฌธ์์ด์ ์ ์ฅํ ๋ณ์ 2๊ฐ, ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ์ ์ ๋ณ์ 1๊ฐ
๋ชจ๋ ํด๋์ค์์ ์ฌ์ฉ๋ ์ ์๋๋ก ์ ์ญ๋ณ์๋ก ์ ์ธํ๋ค.
23์ด~29์ด
activity_main.xml ์ฝ๋์์ ์์ฑํ ์์ ฏ์ ๋ณ์์ ๋์ ํ๋ค.
31์ด
๋ํ๊ธฐ ๋ฒํผ์ ํฐ์นํ์ ๋์ ๋ฆฌ์ค๋๋ฅผ ์ค์ ํ๋ค.
34,35์ด
์๋ํธ ํ ์คํธ์ ์ ๋ ฅ๋ ๊ฐ์ num1, num2 ๋ณ์์ ๋์ ํด์ค๋ค.
36์ด
Integer.parseInt() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ num1, num2๋ฅผ ์ ์ํ์ผ๋ก ๋ณํํ ํ ๋ ๊ฐ์ ๋ํ ํ result ๋ณ์์ ํ ๋นํ๋ค.
37์ด
์ ์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ๋ฌธ์์ด๋ก ๋ณํํ ํ setText() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ํ ์คํธ๋ทฐ์ ๋์ ํ๋ค.
๋นผ๊ธฐ, ๊ณฑํ๊ธฐ, ๋๋๊ธฐ๋ ๋๊ฐ์ ๋ฐฉ์์ผ๋ก ํด์ค๋ค.
AVD ์คํ ๊ฒฐ๊ณผ
๋๊ธ