0
0

Android Studio: Kotlin を使う

Last updated at Posted at 2023-10-01

こちらと同様のことを、Ubuntu 23.04 にインストールした Android Studio Giraffe | 2022.3.1 で行いました。
Let’s Build Your First Android App in Kotlin (Android Studio Giraffe)

Empty Views Activity を選ぶ

image.png

Language は Kotlin にする

image.png

activity_main.xml を次のようにする

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

image.png

MainActivity.kt を次のようにする

MainActivity.kt
package com.example.n003

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlin.random.Random

class MainActivity : AppCompatActivity() {

    private  val list = listOf("東京", "新橋", "品川", "川崎", "横浜", "戸塚", "大船")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button: Button = findViewById(R.id.button)

        button.setOnClickListener {
            val index: Int = Random.nextInt(list.size)
            val randomDogName = list[index]
            Toast.makeText(this,randomDogName, Toast.LENGTH_SHORT).show()
            button.text = randomDogName
        }
    }
}

Run

image.png

image.png

Build APK(s)

右下に、次のウインドーが出ます。
image.png

locate をクリック
image.png

app-debug.apk を USB などで、実機に持って行って、インストールします。
つまり、app-debug.apk はインストーラーです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0