[JAVA] Ich habe eine Janken App mit Kotlin gemacht

Einführung

Ich bin ein Anfänger College-Student mit 2 Monaten Java-Erfahrung und 1 Monat Kotlin-Erfahrung. Ich hoffe, dieser Artikel gibt Ihnen einige Hinweise für ähnliche Anfänger, die versuchen, Apps mit Kotlin zu erstellen.

Die Entwicklungsumgebung ist Android Studio 3.0.1.

Referenzseite https://blog.xsota.com/2013/07/android.html xsota Blog "Machen wir eine Android App-Janken Edition"

Jeder ID-Name ist ・ Bild von Goo: gu ・ Choki Bild: cho ・ Par Bild: per

・ Choki ImageButton: Choki ・ Par ImageButton: per ・ Meine Hand ImageView: playerHand ・ Hand des Gegners ImageView: comHand -Result TextView: Ergebnis Ich mache

Code: kt

MainActivity.kt


package com.example.yusaku.jankenbuttonapp

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : Activity(), View.OnClickListener {
    override fun onClick(v: View?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val gu: ImageButton = findViewById(R.id.gu)
        val choki: ImageButton = findViewById(R.id.choki)
        val per: ImageButton = findViewById(R.id.per)

        gu.setOnClickListener(listener)
        choki.setOnClickListener(listener)
        per.setOnClickListener(listener)
    }


    val listener = object : View.OnClickListener {
        override fun onClick(v: View?) {

            var hands = IntArray(3)
            //Weisen Sie Ihrer Hand eine Bilddatei zu
            hands[0] = R.drawable.gu
            hands[1] = R.drawable.cho
            hands[2] = R.drawable.per

            var playerHand: ImageView = findViewById(R.id.playerHand)
            var comHand: ImageView = findViewById(R.id.comHand)

            val random = Random()
            val n = random.nextInt(3)

            var hand = 0
            //0:gu 1:choki 2:per
            if (v == findViewById(R.id.gu)) {
                hand = 0
            } else if (v == findViewById(R.id.choki)) {
                hand = 1
            } else if (v == findViewById(R.id.per)) {
                hand = 2
            }
            playerHand.setImageResource(hands[hand])
            comHand.setImageResource(hands[n])
            val syouhai = hand - n + 3

            if (syouhai % 3 == 0) {
                result.setText("Aiko")
            } else if (syouhai % 3 == 1) {
                result.setText("Du hast verloren")
            } else if (syouhai % 3 == 2) {
                result.setText("Du gewinnst")
            }

        }
    }
}

Code: xml

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yusaku.jankenbuttonapp.MainActivity">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/result_text"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/playerHand"
        app:layout_constraintEnd_toEndOf="@+id/playerHand"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/playerHand"
        app:layout_constraintTop_toBottomOf="@+id/comHand" />

    <ImageButton
        android:id="@+id/gu"
        android:layout_width="95dp"
        android:layout_height="83dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/gu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/choki"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <ImageButton
        android:id="@+id/choki"
        android:layout_width="98dp"
        android:layout_height="97dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/cho"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/per"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/gu"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageButton
        android:id="@+id/per"
        android:layout_width="95dp"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@drawable/per"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/choki"
        app:layout_constraintTop_toBottomOf="@+id/playerHand"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageView
        android:id="@+id/comHand"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/result"
        app:layout_constraintEnd_toEndOf="@+id/result"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/result"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <ImageView
        android:id="@+id/playerHand"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/choki"
        app:layout_constraintEnd_toEndOf="@+id/choki"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/choki"
        app:layout_constraintTop_toBottomOf="@+id/result"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/player_text"
        app:layout_constraintBottom_toTopOf="@+id/gu"
        app:layout_constraintEnd_toStartOf="@+id/playerHand"
        app:layout_constraintHorizontal_bias="0.555"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.79" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/com_text"
        app:layout_constraintBottom_toTopOf="@+id/per"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.48"
        app:layout_constraintStart_toEndOf="@+id/comHand"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.201" />

</android.support.constraint.ConstraintLayout>

Impressionen

-Die App wurde nicht gestartet, auch wenn ich das, was in Java geschrieben wurde, so wie es war konvertiert habe und der Fehler verschwunden ist. Daher habe ich die Deklaration vor dem Spaß an Create, einer Referenzsite, entfernt und in jeder Methode eine Deklaration erstellt, und es hat funktioniert. ・ Val Listener = Objekt: View.OnClickListener hatte ebenfalls große Probleme. Als Ergebnis der Suche mit setOnClickListener (this) habe ich die folgende Site gefunden. Durch das Setzen des Deklarationsobjekts: scheint die Schnittstellenklasse als Instanz für jede Implementierung beschrieben zu werden. Weitere Informationen finden Sie unter https://dev.classmethod.jp/smartphone/android-kotlin-introduction-02/ ·persönlich override fun onClick(v: View?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } Der Ort ist ebenfalls unbekannt. Dies geschah, als ich es der Fehlerbehandlung von Android Studio überließ. Mangel an Studium.

・ Die Art und Weise, den Code zu schreiben, ist immer noch nicht optimal, daher würde ich gerne einen besseren Weg finden. ・ Weiter ist http://seesaawiki.jp/genesix_android/d/%A4%B8%A4%E3%A4%F3%A4%B1%A4%F3%A5%B2%A1%BC%A5%E0%A4% F2% BA% EE% A4% EB Ich möchte etwas Kompliziertes ausprobieren, einschließlich Wiederholungen, in Bezug auf.

Recommended Posts

Ich habe eine Janken App mit Kotlin gemacht
Ich habe eine Janken App mit Android gemacht
Ich habe eine shopify App @java erstellt
Ich habe mit Swing eine GUI erstellt
Ich habe eine passende App erstellt (Android App)
[Android] Ich habe eine Schrittzähler-App erstellt.
Ich habe eine Taschenrechner-App für Android erstellt
[Rails] Ich habe eine einfache Kalender-Mini-App mit benutzerdefinierten Spezifikationen erstellt.
04. Ich habe mit SpringBoot + Thymeleaf ein Frontend gemacht
Ich habe Mosaikkunst mit Pokemon-Bildern gemacht
Ich habe ein Janken-Spiel in Java (CLI) gemacht.
Ich habe eine Viewer-App erstellt, die PDF anzeigt
Ich habe einen LINE Bot mit Rails + Heroku gemacht
Ich habe mit Ruby On Rails ein Portfolio erstellt
Erstellen Sie eine Chat-App mit WebSocket (Tyrus) + libGDX + Kotlin
Ich habe mit der Lautstärketaste mit der Android-App ein Sperrmuster erstellt. Fragment Edition
Ich habe eine Entwicklungsumgebung mit Rails6 + Docker + PostgreSQL + Materialise erstellt.
[Rails] Ich habe versucht, eine Mini-App mit FullCalendar zu erstellen
Ich möchte eine mit Rails 6 erstellte App an GitHub senden
Ich habe ein Plug-In erstellt, das Jextract mit Gradle-Aufgaben ausführt
Ich möchte eine Liste mit Kotlin und Java erstellen!
Ich möchte eine Funktion mit Kotlin und Java erstellen!
Ich habe einen MOD erstellt, der sofort ein Fahrzeug mit Minecraft anruft
Erstellen einer Timer-App mit Schlamm
Ich habe eine einfache Empfehlungsfunktion erstellt.
Ich habe ein Tool zur Generierung von package.xml erstellt.
Ich habe mit JD-Core eine Befehlszeilenschnittstelle mit dem WinMerge Plugin erstellt
Ich habe kürzlich eine JS-App in der gemunkelten Dart-Sprache erstellt
[Ruby] Ich habe einen einfachen Ping-Client erstellt
Ich habe einen Öko-Server mit Scala gemacht
Ich habe versucht, ein wenig mit BottomNavigationView zu spielen ①
Ich habe ein Plug-In für IntelliJ IDEA erstellt
[Rails 5] Erstelle eine neue App mit Rails [Anfänger]
Ich habe ein neues Java-Bereitstellungstool erstellt
[LINE BOT] Ich habe einen Ramen BOT mit Java (Maven) + Heroku + Spring Boot (1) gemacht.
Ich habe mit Vue.js eine Seite erstellt, die Informationen zur Zuckereinschränkung zusammenfasst
Ich habe ein Programm zur Beurteilung von Primzahlen in Java erstellt
Erstellen Sie mit Spring Boot eine einfache Such-App
Ich habe StringUtils.isBlank gemacht
Ich habe selbst eine App gemacht! (Leseverwaltungs-App)
Veröffentlichen Sie die mit Ruby on Rails erstellte App
Erstellen Sie eine Kotlin-App mit dem OpenJDK Docker-Container
Ich habe mit Ruby einen Blackjack gemacht (ich habe versucht, Minitest zu verwenden)
Ich habe eine Android-App für den MiRm-Dienst erstellt
Ich habe versucht, eine LINE-Klon-App zu erstellen
Ich habe einen Docker-Container erstellt, um Maven auszuführen
Ich habe eine Ruby-Erweiterungsbibliothek in C erstellt
[Rails] Ich habe eine Entwurfsfunktion mit enum erstellt
Ich habe Code Pipeline mit AWS CDK erstellt.
Lerne Java mit Progate → Ich werde es erklären, weil ich selbst ein einfaches Spiel gemacht habe
Ein einfaches Stein-Papier-Scheren-Spiel mit JavaFX und SceneBuilder
Ich habe versucht, den Block mit Java zu brechen (1)
Ich habe eine Funktion zum Registrieren von Bildern bei der API in Spring Framework erstellt. Teil 1 (API Edition)
Ich habe ein Docker-Image für die japanische Version von SDAPS erstellt