Android Application

Kotlin 클래스

작성자 임베디드코리아 작성일23-08-27 20:46 조회1,705회 댓글0건

첨부파일

----------------------------------------------------------------------------------------
    MainActivity..kt
----------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var BTN01: Button
    private lateinit var BTN02: Button
    private lateinit var BTN03: Button
    private lateinit var TV: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        BTN01= findViewById(R.id.Button01)
        BTN02= findViewById(R.id.Button02)
        BTN03= findViewById(R.id.Button03)
        TV= findViewById(R.id.TextTV)

        val smartTvDevice = SmartDevice("Android Tv","Entertainment")

        //smartTvDevice.turnOn()
        //smartTvDevice.turnOff()

        BTN01.setOnClickListener  {
            TV.setText("Device name is: ${smartTvDevice.name}")
            Toast.makeText(applicationContext, "Device name is: ${smartTvDevice.name}", Toast.LENGTH_LONG).show()
            println("Device name is: ${smartTvDevice.name}")
        }

        BTN02.setOnClickListener  {

            smartTvDevice.turnOn()

        }

        BTN03.setOnClickListener  {

            smartTvDevice.turnOff()

        }


    }
}

class SmartDevice(val name: String, val category: String): AppCompatActivity() {

    var deviceStatus = "online"

    fun turnOn(){
        //Toast.makeText(this@SmartDevice,"Smart device is turned on.", Toast.LENGTH_LONG).show()
        println("Smart device is turned on.")
    }

    fun turnOff(){
        //Toast.makeText(this@SmartDevice, "Smart device is turned on.", Toast.LENGTH_LONG).show()
        println("Smart device is turned off.")
    }

}

----------------------------------------------------------------------------------------
    activity_main.xml
----------------------------------------------------------------------------------------
<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/TextTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android Kotlin"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp"
            />

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            />

        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            />

        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>