I am a beginner college student with 2 months of java experience and 1 month of kotlin experience. I hope this article is a hint for similar beginners trying to build apps using kotlin.
The development environment is android studio 3.0.1.
Reference site https://blog.xsota.com/2013/07/android.html xsota blog "Let's make an Android application-rock-paper-scissors edition"
Each id name is ・ Image of gu: gu ・ Choki image: cho ・ Par image: per ・ GU ImageButton: gu ・ Choki ImageButton: choki ・ Par ImageButton: per ・ My hand ImageView: playerHand ・ Opponent's hand ImageView: comHand -Result TextView: result I'm doing
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)
//Assign an image file to your hand
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("You lost")
} else if (syouhai % 3 == 2) {
result.setText("You win")
}
}
}
}
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>
-The app did not start even if I converted what was written in java as it was and the error disappeared. Therefore, I removed the declaration before fun on Create, which was a reference site, and made a declaration in each method, and it worked. ・ Val listener = object: View.OnClickListener also struggled quite a bit. I found the following site as a result of searching without going well with setOnClickListener (this). By putting the declaration object :, it seems that the interface class can be described as an instance for each implementation. For more information https://dev.classmethod.jp/smartphone/android-kotlin-introduction-02/ ·personally override fun onClick(v: View?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } The place is also unknown. It became like this when I left it to the error handling of android studio. Lack of study.
・ The way to write the code is still not optimal, so I would like to find a better way. ・ Next is 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 I would like to try something a little complicated, including retries, with reference to.
Recommended Posts