반응형
Notice
Recent Posts
Link
«   2024/12   »
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
Archives
Today
Total
관리 메뉴

500error

[안드로이드, Kotlin] 단위변환기 앱 만들기 - 개인 프로젝트 2 본문

안드로이드/코틀린

[안드로이드, Kotlin] 단위변환기 앱 만들기 - 개인 프로젝트 2

Internal Server Error 2024. 1. 4. 15:56
반응형

개요

  • 입출력 단위 조정 가능
  • swap버튼을 통해 단위 변경
  • 값을 입력하면 바로 변환

 

 

UI

xml

<pre id="code_1637046272621" class="xml" data-ke-laguage="xml"><code>

<?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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/inputNumber"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="자연수를 입력해주세요"
        android:textSize="20sp"
        android:textStyle="italic"
        android:gravity="end"
        android:maxLength="9"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintVertical_bias="0.4"/>

    <TextView
        android:id="@+id/outputNumber1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="100"
        android:gravity="end"
        android:textSize="30sp"
        android:layout_marginTop="30dp"
        app:layout_constraintEnd_toEndOf="@id/inputNumber"
        app:layout_constraintStart_toStartOf="@id/inputNumber"
        app:layout_constraintTop_toBottomOf="@id/inputNumber"/>


    <TextView
        android:id="@+id/inputUnitNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cm"
        android:textSize="20sp"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toEndOf="@id/inputNumber"
        app:layout_constraintTop_toTopOf="@id/inputNumber"
        app:layout_constraintBottom_toBottomOf="@id/inputNumber"/>
    
    
    <TextView
        android:id="@+id/outputUnitNumber1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="m"
        app:layout_constraintStart_toEndOf="@id/outputNumber1"
        app:layout_constraintBaseline_toBaselineOf="@id/outputNumber1"/>





    <ImageButton
        android:id="@+id/swapBotton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/baseline_swap_vert_24"
        app:layout_constraintTop_toTopOf="@id/inputNumber"
        app:layout_constraintBottom_toBottomOf="@id/outputNumber1"
        app:layout_constraintStart_toEndOf="@id/inputUnitNumber"
        android:layout_marginStart="16dp" />




</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

 

 

Main Activity

package com.exemple.chapter2

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.widget.addTextChangedListener
import com.exemple.chapter2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)

        val view = binding.root

        setContentView(view)

        val outputNumber = binding.outputNumber1
        val outputUnitNumber = binding.outputUnitNumber1
        val inputNumber = binding.inputNumber
        val inputUnitNumber = binding.inputUnitNumber
        val swapImageButton = binding.swapBotton


        var inputNum : Int = 0
        var cmToM = true
        var cmToKm = true

        inputNumber.addTextChangedListener{ text ->
            inputNum = if (text.isNullOrEmpty()) {
                0
            } else {
                text.toString().toInt()
            }

            var outputNum = inputNum.times(0.01)
            if (cmToM) {
                outputNumber.text = inputNum.times(0.01).toString()
            } else {
                outputNumber.text = inputNum.times(100).toString()
            }

            outputNumber.text = outputNum.toString()

        }

        swapImageButton.setOnClickListener {
            cmToM = cmToM.not()
            if (cmToM) {
                inputUnitNumber.text = "cm"
                outputUnitNumber.text = "m"
                outputNumber.text = inputNum.times(0.01).toString()
            }
            else {
                inputUnitNumber.text = "m"
                outputUnitNumber.text = "cm"
                outputNumber.text = inputNum.times(100).toString()
            }

        }

    }
}

 

Main Activity의 패키지 부분은 각자 하고있는 파일의 위치로 바꿔주세요!

반응형
Comments