모바일프로그래밍 - <간단한 숫자 계산 앱>

2023. 3. 14. 11:41·수업

3/14 모바일프로그래밍 수업

 

Kotlin, Android Studio 사용

 

 

항상 화면 설계 제일 처음 작업할 때,

resource > layout > activity_main.xml

로 들어가서 한다.

 

 

 

<간단한 숫자 계산 앱>

1. Android Studio에서 프로젝트 하나를 생성한다.

 

2. activity_main.xml에서 다음과 같이 디자인한다.

 

 

- 값입력 : Text->Number

- 등급선택 : Text->TextView

- 라디오버튼 : Buttons > RadioGroup 넣고 난 후 RadioButton 3개 넣기

- 올림 : Button > Switch

- 결과 : Text -> TextView

 

3. 코드

 

MainActivity class

package com.example.gradetest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.RadioButton
import android.widget.Switch
import android.widget.TextView
import kotlin.math.roundToInt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val price: TextView = findViewById(R.id.txtPrice)
        val result: TextView = findViewById(R.id.txtResult)

        val gra1: RadioButton = findViewById(R.id.rbGrade1)
        val gra2: RadioButton = findViewById(R.id.rbGrade2)
        val gra3: RadioButton = findViewById(R.id.rbGrade3)

        val calc: Button = findViewById(R.id.btnOK)
        val swr: Switch = findViewById(R.id.swRound)

        calc.setOnClickListener{
            val pr = price.text.toString().toDouble()
            var res: Double = pr
            if (gra1.isChecked) res += pr * 0.20
            if (gra2.isChecked) res += pr * 0.15
            if (gra3.isChecked) res -= pr * 0.10

            if (swr.isChecked){
                res = kotlin.math.ceil(res)
            }

            result.text = res.roundToInt().toString()
        }
    }
}

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <EditText
        android:id="@+id/txtPrice"
        android:layout_width="282dp"
        android:layout_height="66dp"
        android:layout_marginStart="44dp"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:hint="원래 가격 입력"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="175dp"
        android:layout_height="34dp"
        android:layout_marginStart="56dp"
        android:layout_marginTop="32dp"
        android:text="등급을 선택"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtPrice" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="293dp"
        android:layout_height="145dp"
        android:layout_marginTop="68dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <RadioButton
            android:id="@+id/rbGrade1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="최상위(20%)" />

        <RadioButton
            android:id="@+id/rbGrade2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="보통(15%)" />

        <RadioButton
            android:id="@+id/rbGrade3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="최하위(10%)" />
    </RadioGroup>

    <Switch
        android:id="@+id/swRound"
        android:layout_width="263dp"
        android:layout_height="71dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="80dp"
        android:text="올림"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="20dp"
        android:text="계산"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/swRound" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="107dp"
        android:layout_height="49dp"
        android:layout_marginStart="164dp"
        android:layout_marginTop="28dp"
        android:text="결과"
        android:textSize="34sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnOK" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

결과 화면

 

 

 

 

 

 

 

 

 

 

'수업' 카테고리의 다른 글

컴퓨터그래픽스 - 3. 컴퓨터 그래픽스 컬러모델  (2) 2023.03.19
객체지향소프트웨어공학 - 시스템공학과 소프트웨어공학  (0) 2023.03.19
컴퓨터그래픽스 - 2. 컴퓨터 그래픽스 하드웨어  (0) 2023.03.14
모바일프로그래밍 기초 - Android studio 활용 <간단한 주사위 게임 앱 만들기>  (0) 2023.03.14
컴퓨터그래픽스 - 1. 컴퓨터 그래픽스 개요  (0) 2023.03.14
'수업' 카테고리의 다른 글
  • 컴퓨터그래픽스 - 3. 컴퓨터 그래픽스 컬러모델
  • 객체지향소프트웨어공학 - 시스템공학과 소프트웨어공학
  • 컴퓨터그래픽스 - 2. 컴퓨터 그래픽스 하드웨어
  • 모바일프로그래밍 기초 - Android studio 활용 <간단한 주사위 게임 앱 만들기>
eunslog
eunslog
코딩 잘하는 개발자가 꿈입니다. 꾸준히 열심히 코딩공부를 하고 있습니다.
  • eunslog
    오늘도 코딩
    eunslog
  • 전체
    오늘
    어제
    • 분류 전체보기 (93)
      • 일상 (0)
      • 코딩 (31)
        • Spring (1)
        • Database (3)
        • Server (8)
        • Error (11)
        • Git (2)
        • NodeJS (0)
      • SQL (0)
      • 수업 (34)
      • IT 관련 (7)
      • 자격증 (11)
      • 멘토링 (9)
      • 그외 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • github 주소
  • 공지사항

  • 인기 글

  • 태그

    코틀린
    CSS
    자바 기초
    안드로이드 스튜디오
    html
    Kotlin
    AWS 서버
    컴퓨터그래픽스
    멋사
    자바
    안드로이드스튜디오
    멋쟁이사자처럼
    웹프로그래밍
    java 기초
    멋쟁이사자처럼 11기
    멋사 11기
    java
    Android Studio
    모바일프로그래밍
    androidStudio
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
eunslog
모바일프로그래밍 - <간단한 숫자 계산 앱>
상단으로

티스토리툴바