안드로이드

안드로이드 프로그래밍 - 다용도 앱 만들기 8

2023. 1. 18. 18:17
728x90
  • 앱의 기능 설명
    • 1. 로그인 기능
    • 2. 메모장
    • 3. 달력
    • 4. 연락처
    • 5. 그림판
    • 6. 미니게임
    • 7. 계산기
    • 8. 스톱워치
    • 9. 가계부

 

  • 계산기

계산기

 

  • 계산기 xml 코드
더보기
<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:background="@color/colorPrimary"
    android:orientation="vertical"
    android:padding="7dp">

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="2dp"
        android:background="@android:color/holo_orange_light"
        android:padding="5dp"
        android:textColor="#8E24AA"
        android:textSize="30dp"
        tools:ignore="SpeakableTextPresentCheck" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TableRow
            android:layout_margin="1dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/number1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="1"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="2"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="3"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/plus"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="+"
                android:textColor="#FFFFFF"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/number4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="4"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="5"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="6"
                android:textColor="#040404"
                android:textSize="25dp" />

            <Button
                android:id="@+id/sub"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="-"
                android:textColor="#FFFFFF"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/number7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="7"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="8"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/number9"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="9"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/mul"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="*"
                android:textColor="#FFFFFF"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/number0"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="0"
                android:textColor="#000000"
                android:textSize="25dp" />

            <Button
                android:id="@+id/ndot"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="."
                android:textColor="#FFFFFF"
                android:textSize="30dp" />

            <Button
                android:id="@+id/clear"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="clear"
                android:textColor="#FFFFFF"
                android:textSize="20dp" />

            <Button
                android:id="@+id/div"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#06D1BC"
                android:onClick="onClick"
                android:text="/"
                android:textColor="#FFFFFF"
                android:textSize="25dp" />
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/Ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@android:color/holo_orange_light"
        android:onClick="onClick"
        android:text="확인"
        android:textColor="#8E24AA"
        android:textSize="30dp" />

</LinearLayout>
  • TableLayout을 통하여 TableRow를 사용하였다.
  • 계산기와 같이 숫자와 연산자들을 입력한 후, 마진을 통하여 콘텐츠 사이의 간격을 두었다.
  • 확인 버튼을 통하여 연산이 이루어진 결과를 보이도록 하였다.

 

 

 

  • 계산기 java 코드
더보기
public class Activity8 extends AppCompatActivity {
    private TextView text;
    private double storedValue;
    private char curOperator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout8);
        setTitle("Developer 계산기");

        text = findViewById(R.id.number);
    }

    public void onClick(View view) {
        String current = text.getText().toString();

        switch (view.getId()) {
            case R.id.number0:
                text.setText(current + "0");
                break;
            case R.id.number1:
                text.setText(current + "1");
                break;
            case R.id.number2:
                text.setText(current + "2");
                break;
            case R.id.number3:
                text.setText(current + "3");
                break;
            case R.id.number4:
                text.setText(current + "4");
                break;
            case R.id.number5:
                text.setText(current + "5");
                break;
            case R.id.number6:
                text.setText(current + "6");
                break;
            case R.id.number7:
                text.setText(current + "7");
                break;
            case R.id.number8:
                text.setText(current + "8");
                break;
            case R.id.number9:
                text.setText(current + "9");
                break;
            case R.id.ndot:
                text.setText(current + ".");
                break;
            case R.id.plus:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '+';
                    text.setText("");
                    break;
                }
            case R.id.sub:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '-';
                    text.setText("");
                    break;
                }
            case R.id.mul:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '*';
                    text.setText("");
                    break;
                }
            case R.id.div:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '/';
                    text.setText("");
                    break;
                }

            case R.id.clear:
                text.setText("");
                storedValue = 0.0;
                break;

            case R.id.Ok:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    double result = 0;
                    double thisValue = Double.parseDouble(text.getText().toString());
                    switch (curOperator) {
                        case '+':
                            result = storedValue + thisValue;
                            break;
                        case '-':
                            result = storedValue - thisValue;
                            break;
                        case '*':
                            result = storedValue * thisValue;
                            break;
                        case '/':
                            result = storedValue / thisValue;
                            break;
                    }
                    text.setText("" + result);
                    storedValue = 0.0;
                    break;
                }
        }
    }
}

 

  • switch-case 문을 사용하여, 해당 버튼이 클릭 되었을 때, textView에 입력 되도록 한다.
    • plus,sub,mul,div
    • 버튼 클릭시, 먼저 숫자가 입력 되지 않으면 숫자를 먼저 입력하라는 tosat메시지가 뜨게된다.
    • 값을 입력 받으면 Double로 받아 들여 저장한다.
    • curOperator 연산자 실행
    • clear : 저장된 값을 0.0 으로 리셋
    • ok : switch(curOperator)-case 문을 통해 +,-,*,/ 연산을 실행한다.
728x90
저작자표시 비영리 변경금지 (새창열림)

'안드로이드' 카테고리의 다른 글

안드로이드 프로그래밍 - 다용도 앱 만들기 10  (1) 2023.01.18
안드로이드 프로그래밍 - 다용도 앱 만들기 9  (0) 2023.01.18
안드로이드 프로그래밍 - 다용도 앱 만들기 7  (0) 2023.01.18
안드로이드 프로그래밍 - 다용도 앱 만들기 6  (0) 2023.01.18
안드로이드 프로그래밍 - 다용도 앱 만들기 5  (0) 2023.01.18
'안드로이드' 카테고리의 다른 글
  • 안드로이드 프로그래밍 - 다용도 앱 만들기 10
  • 안드로이드 프로그래밍 - 다용도 앱 만들기 9
  • 안드로이드 프로그래밍 - 다용도 앱 만들기 7
  • 안드로이드 프로그래밍 - 다용도 앱 만들기 6
IT의 큰손
IT의 큰손
IT계의 큰손이 되고 싶은 개린이의 Log 일지
Developer Story HouseIT계의 큰손이 되고 싶은 개린이의 Log 일지
IT의 큰손
Developer Story House
IT의 큰손
전체
오늘
어제
  • 분류 전체보기 (457)
    • 정보처리기사 필기 (18)
    • 정보처리기사 실기 (12)
    • 정보처리기사 통합 QUIZ (12)
    • 빅데이터 (11)
    • 안드로이드 (11)
    • 웹페이지 (108)
    • 자바 (49)
    • SQLD (3)
    • 백준 알고리즘 (76)
    • 데이터베이스 (41)
    • 깃허브 (2)
    • Library (14)
    • Server (31)
    • 크롤링&스크래핑 (3)
    • Spring (23)
    • Vue.js (13)
    • React (27)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Developer Stroy House

인기 글

태그

  • DBA
  • 코딩테스트
  • jsp
  • 자바
  • 백준
  • React
  • 데이터베이스
  • IT자격증
  • 알고리즘
  • IT개발자
  • 개발블로그
  • 앱개발자
  • css
  • jquery
  • DB
  • 정보처리기사필기
  • 정보처리기사
  • 웹페이지
  • 개발자
  • 정보보안전문가
  • html
  • 프론트엔드
  • it
  • 백엔드
  • ajax
  • 웹개발
  • 웹개발자
  • IT자격증공부
  • JavaScript
  • java

최근 댓글

최근 글

Designed By hELLO
IT의 큰손
안드로이드 프로그래밍 - 다용도 앱 만들기 8
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.