Created
January 17, 2022 14:33
-
-
Save behrends/25d7a65a7f061af5cb6fb93fc1e9864d to your computer and use it in GitHub Desktop.
TIF19 - Button mit Toast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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