Zapisy na wycieczkę: przykładowe rozwiązanie

Projekt w stylu egzaminu INF.04 - Aplikacje mobilne: Android krok po kroku (android)

Pełna aplikacja w stylu arkusza: pole tekstowe, lista rozwijana, pole wyboru, przycisk i podsumowanie. Kod najpierw sprawdza dane (puste pole = Toast i `return`), potem liczy cenę i pokazuje wynik. `Log.i` zapisuje wpis w Logcat: tak sprawdza się działanie aplikacji bez zmieniania ekranu.

{"layout":"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:padding=\"16dp\">\n\n    <TextView\n        android:id=\"@+id/title\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Zapisy na wycieczkę\"\n        android:textSize=\"24sp\"\n        android:textStyle=\"bold\" />\n\n    <EditText\n        android:id=\"@+id/name\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"Imię i nazwisko\"\n        android:inputType=\"text\" />\n\n    <Spinner\n        android:id=\"@+id/place\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:entries=\"@array/places\" />\n\n    <CheckBox\n        android:id=\"@+id/insurance\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Ubezpieczenie (+40 zł)\" />\n\n    <Button\n        android:id=\"@+id/send\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Zapisz\" />\n\n    <TextView\n        android:id=\"@+id/summary\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\"\n        android:textSize=\"18sp\" />\n\n</LinearLayout>\n","code":"package com.example.app;\n\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.widget.*;\nimport androidx.appcompat.app.AppCompatActivity;\n\npublic class MainActivity extends AppCompatActivity {\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        EditText name = findViewById(R.id.name);\n        Spinner place = findViewById(R.id.place);\n        CheckBox insurance = findViewById(R.id.insurance);\n        TextView summary = findViewById(R.id.summary);\n        findViewById(R.id.send).setOnClickListener(v -> {\n            String person = name.getText().toString().trim();\n            if (person.isEmpty()) {\n                Toast.makeText(this, \"Podaj imię i nazwisko\", Toast.LENGTH_SHORT).show();\n                return;\n            }\n            int price = 300 + (insurance.isChecked() ? 40 : 0);\n            summary.setText(person + \": \" + place.getSelectedItem() + \", \" + price + \" zł\");\n            Log.i(\"Wycieczka\", \"Zapisano: \" + person);\n        });\n    }\n}\n","strings":"<resources>\n    <string name=\"app_name\">Wycieczka</string>\n    <string-array name=\"places\">\n        <item>Zakopane</item>\n        <item>Gdańsk</item>\n        <item>Kraków</item>\n    </string-array>\n</resources>\n"}

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu Aplikacje mobilne: Android krok po kroku·Zaloguj się, aby zapisać postęp na koncie.