개발공부/개발하다_발견함

안드로이드 스튜디오 (코틀린 홀짝)

맙소사 2021. 4. 23. 17:11

 

HollActivity.kt 

package com.example.hellokt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import java.util.*


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

        var mBtnholl = findViewById<Button>(R.id.mBtnholl)
        var mEtRes :EditText = findViewById(R.id.mEtRes)

        mBtnholl.setOnClickListener {

            val num = Math.random()*2

            var mEtCom : EditText = findViewById(R.id.mEtCom)

            var comp = ""
            if(num.toInt() == 0){
                comp = "짝"
                mEtCom.setText("짝")
            } else if (num.toInt() == 1 ) {
                comp = "홀"
                mEtCom.setText("홀")
            }


            var mEtMine : EditText = findViewById(R.id.mEtMine)

            if(comp.equals(mEtMine.getText().toString())){
                Log.d("컴퓨터",comp)
                Log.d("나", mEtMine.getText().toString())
                mEtRes.setText("승리!")
            } else {
                Log.d("컴퓨터",comp)
                Log.d("나", mEtMine.getText().toString())
                mEtRes.setText("패배!")
            }
        }
    }
}

 

activity_holl.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=".HollActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="357dp"
        tools:layout_editor_absoluteY="250dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/mTvCom"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Computer"
                android:textSize="24sp" />

            <EditText
                android:id="@+id/mEtCom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/mTvMine"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Mine"
                android:textSize="24sp" />

            <EditText
                android:id="@+id/mEtMine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/mTvRes"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Result"
                android:textSize="24sp" />

            <EditText
                android:id="@+id/mEtRes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="" />
        </LinearLayout>

        <Button
            android:id="@+id/mBtnholl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Run" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>