Skip to content

Instantly share code, notes, and snippets.

@behrends
Created January 17, 2022 15:04
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/9defcde377408d5942ef91f524418ba1 to your computer and use it in GitHub Desktop.
Save behrends/9defcde377408d5942ef91f524418ba1 to your computer and use it in GitHub Desktop.
TIF19A - Übung 17.01.22
<?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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editNoteTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textAutoCorrect"
android:text="Titel der Notiz" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speichern" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
<EditText
android:id="@+id/editNoteTitle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textAutoCorrect"
android:text="Titel der anderen Notiz" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speichern der zweiten Notiz" />
</LinearLayout>
package com.example.mynotes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
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 EditText-Element herstellen
// findViewById mit passender Id aufrufen
val editText = findViewById<EditText>(R.id.editNoteTitle)
// 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 {
val title = editText.text.toString()
Toast.makeText(this, title, Toast.LENGTH_LONG).show()
}
val button2 = findViewById<Button>(R.id.button2)
button2.setOnClickListener {
editText.text.clear()
}
val editText2 = findViewById<EditText>(R.id.editNoteTitle2)
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val title = editText2.text.toString()
Toast.makeText(this, "Andere: $title", Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment