Skip to content

Instantly share code, notes, and snippets.

@behrends
Created January 17, 2022 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behrends/25d7a65a7f061af5cb6fb93fc1e9864d to your computer and use it in GitHub Desktop.
Save behrends/25d7a65a7f061af5cb6fb93fc1e9864d to your computer and use it in GitHub Desktop.
TIF19 - Button mit Toast
package com.example.mynotes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Layout wird von der Activity benutzt
// Das Layout wird "inflated" bzw. initialisiert
// View-Objekte zur Laufzeit erstellt
setContentView(R.layout.activity_main)
// Referenz auf Button-Element herstellen
// der Button ist hier ein View-Objekt
// findViewById mit passender Id aufrufen
// Java:
// final Button button = (Button)findViewById(R.id.button);
// val ==> Konstante, var ==> Variable
val button = findViewById<Button>(R.id.button)
// Java button.setOnClickLister( new .... {} )
button.setOnClickListener {
Toast.makeText(this, "Button geklickt!", Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment