1+ /*
2+ * https://studyviewer.com/android-calculator-in-kotlin-using-android-studio/
3+ */
14package com.personalized.calculator
25
36import androidx.appcompat.app.AppCompatActivity
@@ -10,7 +13,7 @@ import net.objecthunter.exp4j.ExpressionBuilder
1013
1114class MainActivity : AppCompatActivity () {
1215
13- lateinit var outputTextView: TextView
16+ // lateinit var outputTextView: TextView
1417 var lastNumaric: Boolean = false
1518 var stateError: Boolean = false
1619 var lastDot : Boolean = false
@@ -21,12 +24,13 @@ class MainActivity : AppCompatActivity() {
2124
2225 fun onDigit (view : View )
2326 {
27+ val textView: TextView = findViewById(R .id.textView2) as TextView
2428 if (stateError)
2529 {
26- outputTextView .text= (view as Button ).text
30+ textView .text= (view as Button ).text
2731 stateError= false
2832 }else {
29- outputTextView .append((view as Button ).text)
33+ textView .append((view as Button ).text)
3034 }
3135 lastNumaric= true
3236 }
@@ -35,7 +39,8 @@ class MainActivity : AppCompatActivity() {
3539 {
3640 if (lastNumaric && ! stateError && ! lastDot)
3741 {
38- outputTextView.append(" ." )
42+ val textView: TextView = findViewById(R .id.textView2) as TextView
43+ textView.append(" ." )
3944 lastNumaric= false
4045 lastDot= true
4146 }
@@ -45,7 +50,8 @@ class MainActivity : AppCompatActivity() {
4550 {
4651 if (lastNumaric && ! stateError)
4752 {
48- outputTextView.append((view as Button ).text)
53+ val textView: TextView = findViewById(R .id.textView2) as TextView
54+ textView.append((view as Button ).text)
4955 lastNumaric= false
5056 lastDot= false
5157 }
@@ -54,26 +60,27 @@ class MainActivity : AppCompatActivity() {
5460
5561 fun onClear (view : View )
5662 {
57- this .outputTextView.text= " "
63+ val textView: TextView = findViewById(R .id.textView2) as TextView
64+ textView.text = " "
5865 lastNumaric= false
5966 stateError= false
6067 lastDot= false
6168 }
6269 fun onEqual (view : View )
6370 {
64-
71+ val textView : TextView = findViewById( R .id.textView2) as TextView
6572 if (lastNumaric && ! stateError)
6673 {
67- val text = outputTextView .text.toString()
74+ val text = textView .text.toString()
6875 val expression= ExpressionBuilder (text).build()
6976 try
7077 {
7178 val result= expression.evaluate()
72- outputTextView .text= result.toString()
79+ textView .text= result.toString()
7380 lastDot= true
7481 }catch (ex: Exception )
7582 {
76- outputTextView .text= " Error"
83+ textView .text= " Error"
7784 stateError= true
7885 lastNumaric= false
7986 }
0 commit comments