500error
[안드로이드, Kotlin] 계산기 앱 만들기 - 개인 프로젝트 3 본문
반응형
개요
- 계산기능 구현
뷰바인딩
activity_main.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">
<TextView
android:id="@+id/equationTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="end"
android:text="equation"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/resultTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:padding="16dp"
android:text="result"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@id/KeyPad"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/KeyPad"
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="8dp"
app:constraint_referenced_ids="Button1, Button2, Button3, ButtonClear, Button4, Button5, Button6, ButtonPlus, Button7, Button8, Button9, ButtonMinus, Button0, ButtonEqul"
app:flow_horizontalGap="8dp"
app:flow_maxElementsWrap="4"
app:flow_verticalBias="1"
app:flow_wrapMode="chain"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/Button1"
style="@style/numberKeyPad"
android:text="1"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="287dp" />
<Button
android:id="@+id/Button2"
style="@style/numberKeyPad"
android:text="2"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="162dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/Button3"
style="@style/numberKeyPad"
android:text="3"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button4"
style="@style/numberKeyPad"
android:text="4"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="287dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button5"
style="@style/numberKeyPad"
android:text="5"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="162dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button6"
style="@style/numberKeyPad"
android:text="6"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button7"
style="@style/numberKeyPad"
android:text="7"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="287dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button8"
style="@style/numberKeyPad"
android:text="8"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="162dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button9"
style="@style/numberKeyPad"
android:text="9"
android:onClick="numberClicked"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/Button0"
style="@style/numberKeyPad"
android:text="0"
android:onClick="numberClicked"
app:layout_constraintHorizontal_weight="1"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/ButtonEqul"
style="@style/numberKeyPad"
android:text="="
android:onClick="equalClicked"
app:layout_constraintHorizontal_weight="2"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/ButtonPlus"
style="@style/numberKeyPad"
android:text="+"
android:onClick="operatorClicked"
app:layout_constraintHorizontal_weight="2"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/ButtonMinus"
style="@style/numberKeyPad"
android:text="-"
android:onClick="operatorClicked"
app:layout_constraintHorizontal_weight="2"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
<Button
android:id="@+id/ButtonClear"
style="@style/numberKeyPad"
android:text="C"
android:onClick="clearClicked"
app:layout_constraintHorizontal_weight="2"
tools:layout_editor_absoluteX="37dp"
tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>
맨 윗줄은 티스토리가 xml을 지원하지 않아서 넣어준 코드니 빼주세요
values에 style.xml을 만들어 주세요
style.xml
<pre id="code_1637046272621" class="xml" data-ke-laguage="xml"><code>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="numberKeyPad">
<item name="android:textSize">40sp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:backgroundTint">@color/teal_200</item>
</style>
<color name="teal_200">#00BCD4</color>
</resources>
MainActivity
package com.example.calculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.example.calculator.databinding.ActivityMainBinding
import java.text.DecimalFormat
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val firstNumberText = StringBuilder("")
private val secondNumberText = StringBuilder("")
private val operatorText = StringBuilder("")
private val decimalFormat = DecimalFormat("#, ###")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
fun numberClicked(view: View) {
val numberString = (view as? Button)?.toString() ?: ""
val numberText = if(operatorText.isEmpty()) firstNumberText else secondNumberText
numberText.append(numberString)
updateEquationTextView()
}
fun clearClicked(view : View) {
firstNumberText.clear()
secondNumberText.clear()
operatorText.clear()
updateEquationTextView()
binding.resultTextView.text = ""
}
fun equalClicked(view : View) {
if (firstNumberText.isEmpty() || secondNumberText.isEmpty() || operatorText.isEmpty()) {
Toast.makeText(this, "올바르지 않은 수식입니다.", Toast.LENGTH_SHORT).show()
return
}
val firstNumber = firstNumberText.toString().toBigDecimal()
val secondNumber = secondNumberText.toString().toBigDecimal()
val result = when(operatorText.toString()) {
"+" -> decimalFormat.format(firstNumber + secondNumber)
"-" -> decimalFormat.format(firstNumber - secondNumber)
else ->
"잘못된 수식 입니다."
}.toString()
binding.resultTextView.text = result
}
fun operatorClicked(view : View) {
val operatorString = (view as? Button)?.toString() ?: ""
if (firstNumberText.isEmpty()) {
Toast.makeText(this, "먼저 숫자를 입력해주세요.", Toast.LENGTH_SHORT).show()
return
}
if (secondNumberText.isNotEmpty()) {
Toast.makeText(this, "1개의 연산자에 대해서만 연산이 가능합니다.", Toast.LENGTH_SHORT).show()
return
}
operatorText.append(operatorString)
updateEquationTextView()
}
private fun updateEquationTextView() {
val firstFormattedNumber = if(firstNumberText.isEmpty()) decimalFormat.format(firstNumberText.toString().toBigDecimal()) else ""
val secondFormattedNumber = if(secondNumberText.isEmpty()) decimalFormat.format(secondNumberText.toString().toBigDecimal()) else ""
binding.equationTextView.text = "$firstNumberText $operatorText $secondNumberText"
}
}
반응형
'안드로이드 > 코틀린' 카테고리의 다른 글
error: failed to push some refs to "깃 path" 오류 해결방법 (0) | 2024.10.15 |
---|---|
[안드로이드, Kotlin] 백키 구현 (0) | 2024.10.15 |
[안드로이드, Kotlin] 구글 로그인 구현 (0) | 2024.10.15 |
[안드로이드, Kotlin] 단위변환기 앱 만들기 - 개인 프로젝트 2 (0) | 2024.01.04 |
[안드로이드, Kotlin] 숫자세기(계수기) 앱 만들기 - 개인 프로젝트 1 (1) | 2024.01.04 |
Comments