Android Application

Kotlin 구구단

작성자 임베디드코리아 작성일23-09-18 00:36 조회1,705회 댓글0건

첨부파일

Kotlin  구구단
------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var DanET: EditText
    private lateinit var ExButton: Button
    private lateinit var ResultTV: TextView
    private var result:Int=0
    private var danNum:Int=0

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

        DanET= findViewById(R.id.Dan_ET)
        ExButton = findViewById(R.id.EX_BT)
        ResultTV = findViewById(R.id.result_TV)

        DanET.setOnClickListener  {
            DanET.setText(null)
        }
     
        ExButton.setOnClickListener  {
            var danSTR: String = DanET.text.toString()
            danNum = danSTR.toInt()
            ResultTV.text=""

            for (cnt in 1..9){
                result=danNum*cnt
                ResultTV.append("$danNum * $cnt = $result  \n")
            }

        }


    }
}

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/Dan_ET"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:layout_marginTop="100dp"
        android:background="#C1BDBD"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/EX_BT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="실 행"
        android:layout_marginTop="10dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/result_TV"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="#8CBDA5"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_gravity="center" />

</LinearLayout>