본문 바로가기

안드로이드스튜디오

wordle 앱 제작

728x90
SMALL

위는 아이템들의 정렬을 쉽게 맞춰주는 과정입니다.

 

[game_list.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/etGame1"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Z"
        android:textAlignment="center"
        android:textSize="40sp" />

    <EditText
        android:id="@+id/etGame2"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Z"
        android:textAlignment="center"
        android:textSize="40sp" />

    <EditText
        android:id="@+id/etGame3"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Z"
        android:textAlignment="center"
        android:textSize="40sp" />

    <EditText
        android:id="@+id/etGame4"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Z"
        android:textAlignment="center"
        android:textSize="40sp" />

    <EditText
        android:id="@+id/etGame5"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Z"
        android:textAlignment="center"
        android:textSize="40sp" />
</LinearLayout>

[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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvGame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.437"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

[MainActivity.kt]

package com.example.wordle

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

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

        val answer = "apple"

        //RecyclerView 사용 6단계
        // 1. Container 결정
        val rvGame = findViewById<RecyclerView>(R.id.rvGame)

        // 2. Template 결정
        // game_list.xml

        //3. Item 결정
        val gameList = ArrayList<GameVO>()
        gameList.add(GameVO("","","","",""))
        gameList.add(GameVO("","","","",""))
        gameList.add(GameVO("","","","",""))
        gameList.add(GameVO("","","","",""))
        gameList.add(GameVO("","","","",""))
        gameList.add(GameVO("","","","",""))


        //4. Adatper 결정
        // GameAdapter
        val adapter = GameAdapter(this, gameList, answer)

        //5. Container 에 Adapter 부착
        rvGame.adapter = adapter
        rvGame.layoutManager= LinearLayoutManager(this)



    }
}

[GameAdapter.kt]

package com.example.wordle

import android.content.Context
import android.graphics.Color
import android.provider.UserDictionary.Words
import android.text.Editable
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnKeyListener
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class GameAdapter(val context: Context, val gameList: ArrayList<GameVO>, val answer: String) :
    RecyclerView.Adapter<GameAdapter.ViewHolder>() {


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val etGame1: EditText
        val etGame2: EditText
        val etGame3: EditText
        val etGame4: EditText
        val etGame5: EditText

        init {
            etGame1 = itemView.findViewById(R.id.etGame1)
            etGame2 = itemView.findViewById(R.id.etGame2)
            etGame3 = itemView.findViewById(R.id.etGame3)
            etGame4 = itemView.findViewById(R.id.etGame4)
            etGame5 = itemView.findViewById(R.id.etGame5)

            val etList = ArrayList<EditText>()
            etList.add(etGame1)
            etList.add(etGame2)
            etList.add(etGame3)
            etList.add(etGame4)
            etList.add(etGame5)




            for (i in 0 until 4) {
                etList.get(i).setOnKeyListener(object : OnKeyListener {
                    override fun onKey(p0: View?, p1: Int, p2: KeyEvent?): Boolean {
                        if (p2?.action == KeyEvent.ACTION_DOWN) {
                            etList.get(i + 1).requestFocus()
                        }
                        return false
                    }

                })
            }




            etGame5.setOnKeyListener(object : OnKeyListener {
                override fun onKey(p0: View?, p1: Int, p2: KeyEvent?): Boolean {
                    Log.d("테스트1", p1.toString())
                    Log.d("테스트2", p2.toString())
                    // 66은 엔터
                    if (p1 == 66 && p2?.action == KeyEvent.ACTION_UP) {
                        Log.d("테스트3", "엔터 눌러짐")
//                        checkAnswer(answer, etList)
//                        disableEditText(etList)
                    }

                    return false
                }

            })


        }


    }

    fun enableEditText(etList: ArrayList<EditText>) {
        for (i in 0 until etList.size) {
            etList.get(i).isEnabled = true
        }
    }


    fun disableEditText(etList: ArrayList<EditText>) {
        for (i in 0 until etList.size) {
            etList.get(i).isEnabled = false
        }
    }


    fun checkAnswer(answer: String, etList: ArrayList<EditText>) {
        // a p p l e
        // a l b u m
        // 초 노 회 회 회

        for (i in 0 until etList.size) {
            val answerChar: Char = answer.get(i)
            val etChar: Char = etList.get(i).text.toString().single()

            if (answerChar == etChar) {
                etList.get(i).setBackgroundColor(Color.parseColor("green"))
            } else {
                var check = true

                for (j in 0 until etList.size) {
                    if (etChar == answer.get(j)) {
                        etList.get(i).setBackgroundColor(Color.parseColor("yellow"))
                        check = false
                    }
                }


                if (check) {
                    etList.get(i).setBackgroundColor(Color.parseColor("gray"))
                }
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(context)
        val view = layoutInflater.inflate(R.layout.game_list, null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val etList = ArrayList<EditText>()
        etList.add(holder.etGame1)
        etList.add(holder.etGame2)
        etList.add(holder.etGame3)
        etList.add(holder.etGame4)
        etList.add(holder.etGame5)

        holder.etGame1.setText(gameList.get(position).word1)
        holder.etGame2.setText(gameList.get(position).word2)
        holder.etGame3.setText(gameList.get(position).word3)
        holder.etGame4.setText(gameList.get(position).word4)
        holder.etGame5.setText(gameList.get(position).word5)

//        Log.d("테스트5", position.toString())
//        if(position == 0){
//            enableEditText(etList)
//        }else{
//            disableEditText(etList)
//        }

        holder.etGame5.setOnKeyListener(object : OnKeyListener {
            override fun onKey(p0: View?, p1: Int, p2: KeyEvent?): Boolean {
                if (p1 == KeyEvent.KEYCODE_ENTER && p2?.action == KeyEvent.ACTION_UP) {
                    if (position == gameList.size - 1) {
                        Toast.makeText(context, answer, Toast.LENGTH_SHORT).show()
                    } else {
                        checkAnswer(answer, etList)
                    }
                    disableEditText(etList)
                }
                return false
            }
        })


    }

    override fun getItemCount(): Int {
        return gameList.size
    }


}

[GameVO.kt]

package com.example.wordle

data class GameVO(var word1 : String, var word2: String,
                  var word3: String, var word4: String, var word5:String){

}
728x90
LIST