LoginSignup
4
2

More than 3 years have passed since last update.

湯婆婆をAndroidアプリで実装する

Last updated at Posted at 2020-11-09

本記事は、@NemesisさんのJavaで湯婆婆を実装してみるという記事のパロディです。
湯婆婆をAndroidアプリで実装しました。

必要な機能

・コマンドプロンプト風
・名前が空白だとアプリが落ちる

レイアウト

テキスト表示(TextView)とテキスト入力(EditText)で構成されています。

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"
    android:background="@color/black"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text1"
        tools:ignore="LabelFor" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/name" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text2" />

</androidx.constraintlayout.widget.ConstraintLayout>

内部処理部分

言語はKotlinを使用しています。
大まかに解説すると、最初のテキストを表示し、名前入力部分で入力が終わったらnewName作成+その後のテキスト表示をしています。

MainActivity.kt

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

        text1.text = "契約書だよ。そこに名前を書きな。"
        name.requestFocus()
        name.setOnKeyListener { _, i, keyEvent ->
            if (keyEvent.action == ACTION_DOWN && i == KEYCODE_ENTER) {
                text2.text = "フン。${name.text}というのかい。贅沢な名だねぇ。"
                val newName = name.text[Random.nextInt(name.text.length)].toString()
                text3.text = "今からお前の名前は${newName}だ。いいかい、${newName}だよ。分かったら返事をするんだ、${newName}!!"
                true
            } else {
                false
            }
        }
    }
}
4
2
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
4
2