MainActivity.kt - batooldshilleh/TrainingTasks GitHub Wiki
Introduction:
This is the Kotlin part.
What can we do within this file?
-
Package Declaration:
package com.example.myapplication
: Declares the package name for the application.
-
Imports:
- Import necessary classes from Android framework:
AppCompatActivity
,Bundle
,View
,Button
,EditText
,TextView
.
- Import necessary classes from Android framework:
-
MainActivity Class Declaration:
- Declares a class named
MainActivity
which extendsAppCompatActivity
and implementsView.OnClickListener
interface.
- Declares a class named
-
Properties Declaration:
- Private properties for various UI components:
btnAdd
,btnSub
,btnMultiply
,btnDivision
: Buttons for arithmetic operations.etA
,etB
: EditText fields for inputting numbers.resultTv
: TextView for displaying the result.
- Private properties for various UI components:
-
onCreate() Method:
- Override the
onCreate()
method fromAppCompatActivity
. - Call the superclass's
onCreate()
method and set the content view to the layout defined inactivity_main.xml
. - Initialize UI components by finding their views using
findViewById()
. - Set
OnClickListener
to each button.
- Override the
-
onClick() Method:
- Override the
onClick()
method fromView.OnClickListener
. - Handle click events of buttons.
- Retrieve the input values from
etA
andetB
, convert them to double. - Perform the corresponding arithmetic operation based on the clicked button using a
when
expression. - Update the
resultTv
TextView with the computed result.
- Override the