500error
[안드로이드, Kotlin] 단위변환기 앱 만들기 - 개인 프로젝트 2 본문
반응형
개요
- 입출력 단위 조정 가능
- 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의 패키지 부분은 각자 하고있는 파일의 위치로 바꿔주세요!
반응형
'안드로이드 > 코틀린' 카테고리의 다른 글
error: failed to push some refs to "깃 path" 오류 해결방법 (0) | 2024.10.15 |
---|---|
[안드로이드, Kotlin] 백키 구현 (0) | 2024.10.15 |
[안드로이드, Kotlin] 구글 로그인 구현 (0) | 2024.10.15 |
[안드로이드, Kotlin] 계산기 앱 만들기 - 개인 프로젝트 3 (0) | 2024.02.21 |
[안드로이드, Kotlin] 숫자세기(계수기) 앱 만들기 - 개인 프로젝트 1 (1) | 2024.01.04 |
Comments