Created
January 17, 2022 15:04
-
-
Save behrends/9defcde377408d5942ef91f524418ba1 to your computer and use it in GitHub Desktop.
TIF19A - Übung 17.01.22
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
<?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> |
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.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