{"id":1022,"date":"2024-11-09T07:27:12","date_gmt":"2024-11-09T07:27:12","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=1022"},"modified":"2024-11-09T08:33:56","modified_gmt":"2024-11-09T08:33:56","slug":"kotlin-type-conversion-example","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/11\/09\/kotlin-type-conversion-example\/","title":{"rendered":"[Kotlin]\ud615\ubcc0\ud658\uc608\uc81c2\/Type conversion example2"},"content":{"rendered":"\n<p>\ub354 \uce5c\uc808\ud55c \ucf54\ud2c0\ub9b0 \uc571\ud504\ub85c\uadf8\ub798\ubc0d \ucc45\uc5d0 \uc788\ub294 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>This is code from the Friendlier Kotlin App Programming book<\/p>\n\n\n\n<p>\ud615\ubcc0\ud658 \uc608\uc81c\uc785\ub2c8\ub2e4.<br>This is an example of type conversion.<\/p>\n\n\n\n<p>\uc774 \ucf54\ub4dc\ub294 \ubdf0\ubc14\uc778\ub529\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.<br>This code does not use view binding.<\/p>\n\n\n\n<p>Test : compileSdk = 34 targetSdk = 34<\/p>\n\n\n\n<p><strong>MainActivity.kt<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.app3\n\nimport android.os.Bundle\nimport android.widget.Button\nimport android.widget.EditText\nimport android.widget.TextView\nimport android.widget.Toast\nimport androidx.activity.enableEdgeToEdge\nimport androidx.appcompat.app.AppCompatActivity\nimport androidx.core.view.ViewCompat\nimport androidx.core.view.WindowInsetsCompat\nimport com.example.app3.constants.mBonus\n\nclass MainActivity : AppCompatActivity() {\n\n    var nameInput:String? = null \/\/ nullable type\n    lateinit var mobileInput:String \/\/ lateinit\n\n    var valueInput1:String = \"\"\n    var valueInput2:String = \"\"\n    var firstValue:Int = 0\n    var secondValue:Int = 0\n\n    companion object{\n        const val BONUS = 100\n    }\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContentView(R.layout.activity_main)\n\n        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -&gt;\n            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())\n            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)\n            insets\n        }\n\n\n\n        val button = findViewById&lt;Button&gt;(R.id.button)\n        val name = findViewById&lt;EditText&gt;(R.id.nameInput)\n        val mobile = findViewById&lt;EditText&gt;(R.id.mobileInput)\n\n        val button2 = findViewById&lt;Button&gt;(R.id.button2)\n        val inputtext1 = findViewById&lt;EditText&gt;(R.id.inputText1)\n        val inputtext2 = findViewById&lt;EditText&gt;(R.id.inputText2)\n        val textView1 = findViewById&lt;TextView&gt;(R.id.textView)\n        val textView2 = findViewById&lt;TextView&gt;(R.id.textView2)\n\n\n        button.setOnClickListener(){\n \n            nameInput = name.text.toString()\n            mobileInput = mobile.text.toString()\n            Toast.makeText(applicationContext,\"\uc774\ub984name: $nameInput, \uc804\ud654\ubc88\ud638phoneNumber: $mobileInput\",Toast.LENGTH_LONG).show()\n            \/\/ kotlin style\n            println(\"\uc774\ub984name: \" + nameInput + \"\uc804\ud654\ubc88\ud638phoneNumber: \" + mobileInput)\n            \/\/ java style \n            System.out.println(\"\uc774\ub984name: \" + nameInput + \"\uc804\ud654\ubc88\ud638phoneNumber: \" + mobileInput)\n        }\n\n        button2.setOnClickListener(){\n\n         valueInput1 = inputtext1.text.toString()\n         valueInput2 = inputtext2.text.toString()\n         firstValue =  valueInput1.toInt() \n         secondValue = valueInput2.toInt()\n\n         val result:Any?\n         result  = firstValue + secondValue + MainActivity.BONUS.toInt() + mBonus\n         textView1.text = \"\uacb0\uacfcresult: $result\"\n\n         if(result == 20){\n             textView1.text = \"\ub354\ud558\uae30 \uacb0\uacfc\uac00 20\uc785\ub2c8\ub2e4.\\n addition results in 20\"\n         }\n         if(valueInput1 == \"10\"){\n             textView1.append(\"\\n \uccab\ubc88\uc9f8 \uc785\ub825\ub41c \uac12\uc774 10\uc785\ub2c8\ub2e4.\\n The first entered value is 10.\")\n         }\n\n\n         val input1:Any = \"\uc548\ub155Hello\"\n         val input2:Any = 10\n\n         if(input1 is String){\n             val output2 = input1 as String\n             textView2.setText(\" $output2 \ub294 \ubb38\uc790\uc5f4 \uc790\ub8cc\ud615\uc785\ub2c8\ub2e4.\")\n             \/\/ \ubc14\ub85c\uc704\uc640 \uac19\uc740 \uacb0\uacfc : textView1.text = \"input1\uc740 \ubb38\uc790\uc5f4 \uc790\ub8cc\ud615\uc785\ub2c8\ub2e4.\";\n         }\n\n         if(input2 is Int){\n             val output3:Int? = input2 as Int\n             textView2.append(\"\\n $input2 \ub294 \uc22b\uc790 \uc790\ub8cc\ud615\uc785\ub2c8\ub2e4.\")\n         }\n\n        }\n\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Constants.kt<\/strong>(package variable)<\/p>\n\n\n\n<p>-directory(file location)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"366\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3Directory.png\" alt=\"\" class=\"wp-image-1028\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3Directory.png 435w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3Directory-300x252.png 300w\" sizes=\"auto, (max-width: 435px) 100vw, 435px\" \/><\/figure>\n\n\n\n<p>-kotlin code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.app3.constants\n\nval mBonus:Int = 1000<\/code><\/pre>\n\n\n\n<p><strong>active_main.xml<\/strong><\/p>\n\n\n\n<p>-xml design<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"670\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3.png\" alt=\"\" class=\"wp-image-1027\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3.png 387w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/App3-173x300.png 173w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/figure>\n\n\n\n<p>-xml code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:id=\"@+id\/main\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/nameInput\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"124dp\"\n        android:ems=\"10\"\n        android:hint=\"\uc774\ub984Name\"\n        android:inputType=\"text\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/mobileInput\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"32dp\"\n        android:ems=\"10\"\n        android:hint=\"\uc804\ud654\ubc88\ud638PhoneNumber\"\n        android:inputType=\"text\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.502\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/nameInput\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"44dp\"\n        android:text=\"Show\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/mobileInput\"\n        app:layout_constraintVertical_bias=\"0.0\" \/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/inputText1\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"40dp\"\n        android:ems=\"10\"\n        android:hint=\"\uccab\ubc88\uc9f8 \uac12First Value\"\n        android:inputType=\"text\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.542\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button\" \/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/inputText2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"20dp\"\n        android:ems=\"10\"\n        android:hint=\"\ub450\ubc88\uc9f8 \uac12Second Value\"\n        android:inputType=\"text\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.542\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/inputText1\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"24dp\"\n        android:text=\"\ub354\ud558\uae30Plus\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/inputText2\" \/&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/textView\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"16dp\"\n        android:text=\"\uacb0\uacfcResult\"\n        android:textColorLink=\"#9C27B0\"\n        android:textSize=\"16sp\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.498\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button2\" \/&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/textView2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\uacb0\uacfc2Result2\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/textView\" \/&gt;\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ub354 \uce5c\uc808\ud55c \ucf54\ud2c0\ub9b0 \uc571\ud504\ub85c\uadf8\ub798\ubc0d \ucc45\uc5d0 \uc788\ub294 \ucf54\ub4dc \uc785\ub2c8\ub2e4.This is code from the Friendlier Kotlin App Programming book \ud615\ubcc0\ud658 \uc608\uc81c\uc785\ub2c8\ub2e4.This is an example of type conversion. \uc774 \ucf54\ub4dc\ub294 \ubdf0\ubc14\uc778\ub529\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.This code does not use view binding. Test : compileSdk = 34 targetSdk = 34 MainActivity.kt Constants.kt(package variable) -directory(file location) -kotlin code active_main.xml -xml design -xml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1],"tags":[],"class_list":["post-1022","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=1022"}],"version-history":[{"count":6,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"predecessor-version":[{"id":1044,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions\/1044"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}