Android Application

Kotlin 토스트(Toast)

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

첨부파일

Kotlin    토스트(Toast)
-------------------------------------------------------------------------------------
      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="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_ToastSHORT"
            android:text="POP Toast SHORT"
            android:textAllCaps="false"
            android:padding="15dp"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn_ToastLONG"
            android:text="POP Toast LONG"
            android:textAllCaps="false"
            android:padding="15dp"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var btnToastshort : Button = findViewById(R.id.btn_ToastSHORT)
        var btnToastlong : Button = findViewById(R.id.btn_ToastLONG)

        btnToastshort.setOnClickListener{
            // Toast.makeText(this, "Toast LENGTH_SHORT", Toast.LENGTH_SHORT).show()
            Toast.makeText(applicationContext, "Toast LENGTH_SHORT", Toast.LENGTH_SHORT).show()
            //Toast.makeText(this@MainActivity, "Toast LENGTH_SHORT", Toast.LENGTH_SHORT).show()
        }

        btnToastlong.setOnClickListener{
            // Toast.makeText(this, "Toast LENGTH_LONG", Toast.LENGTH_LONG).show()
            Toast.makeText(applicationContext, "Toast LENGTH_LONG", Toast.LENGTH_LONG).show()
            //Toast.makeText(this@MainActivity, "Toast LENGTH_LONG", Toast.LENGTH_LONG).show()
        }
    }
}