ref. 안드로이드프로그래밍 of 한빛미디어
초간단 계산기 만들기
실습 목표 결과
우선 보이는 것과 같이 구성하기 위하여
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
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Edit1" <----닉네임 설정
android:hint="@string/str1" <---- string.xml 에서 str1의 텍스트를 불러옴
android:layout_margin="10dp"
/>
~~~~~~~~~~~~~~생략~~~~~~~~~~~~~~~~~
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd"
android:text="더하기"
android:layout_margin="10dp" <----다른 위젯이나 레이아웃과 거리를 둠
/>
~~~~~~~~~~~~~~생략~~~~~~~~~~~~~~~~~
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextResult"
android:text="계산 결과 : "
android:layout_margin="10dp"
android:textSize="30dp" <----글씨 크기
android:textColor="#FF0000" <---글씨색 (빨강) #RRGGBB 진하기 최대값 FF
/>
이후 MainActivity로 넘어가 Java로 코딩한다.
우선 다음과 같이 변수를 선언한다.
그 후
메소드를 구성한다.
변수 대입
이후에 각 버튼 터치시 동작을 설정해준다.
동작설정
각 뺴기 곱하기 나누기 등은
1
result = Integer.parseInt(num1) + Integer.parseInt(num2);
부분에서 +를 각 -,*,/,% 로 바꿔준다.