๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ป Extracurricular/์ฝ”๋ฎค๋‹ˆํ‹ฐ ๋ชจ๊ฐ์ฝ”

๋ชจ๊ฐ์ฝ” < Kotlin ๊ธฐ์ดˆํŽธ > - 11์ผ์ฐจ

โœ… ์˜ค๋Š˜ ๋ฐฐ์šธ ๋‚ด์šฉ

  1. EditText ์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
  2. ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌํ•˜๋Š” ์กฐ๊ฑด๋ฌธ ๋งŒ๋“ค๊ธฐ

โœ… EditText์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

๐Ÿ“› ์ฃผ์˜ ๐Ÿ“›

Edit Text ์ปดํฌ๋„ŒํŠธ์˜ text๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š”

๋’ค์— ๊ผญ .toString()์„ ๋ถ™์—ฌ์•ผ ํ•จ!

// EditText ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐพ์•„์„œ editText ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
val editText = findViewById<EditText>(R.id.์•„์ด๋””)

// editText ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€ passwordText ์— ์ €์žฅํ•œ๋‹ค.
val passwordText = editText.text.toString()

EditText๋Š”?

์ผ๋ฐ˜ TextView โŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ โญ•

text ์†์„ฑ์ด ์ผ๋ฐ˜ textView์™€ ๋‹ฌ๋ฆฌ EditText ๋งŒ์˜ ํ˜•ํƒœ๋กœ ์„ค์ •๋จ.

๋”ฐ๋ผ์„œ ๊ทธ ์•ˆ์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด toString() ์„ ๋ถ™์—ฌ์•ผ ํ•จ!!


โœ… ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌํ•˜๋Š” ์กฐ๊ฑด๋ฌธ ๋งŒ๋“ค๊ธฐ

์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•ด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํ…์ŠคํŠธ(๋น„๋ฐ€๋ฒˆํ˜ธ)๊ฐ€ ๋งž๋Š”์ง€ ์ฒดํฌํ•˜๊ธฐ!

 

๐Ÿ“› == ์—ฐ์‚ฐ์ž?

์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๋Š” ์—ฐ์‚ฐ์ž

=> if (์กฐ๊ฑด๋ฌธ) ์—์„œ ์กฐ๊ฑด๋ฌธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ ์‹คํ–‰

if ( passwordText == "12345678" ) 
    // ์ฐธ์ด๋ผ๋ฉด(์ž…๋ ฅํ•œ passwordText ์™€ "12345678"์ด ๊ฐ™๋‹ค๋ฉด) ์ด๊ณณ์˜ ๋ช…๋ น ์‹คํ–‰
} else {
   // ๊ฑฐ์ง“์ด๋ผ๋ฉด(๋‹ค๋ฅด๋‹ค๋ฉด) ์ด๊ณณ์˜ ๋ช…๋ น ์‹คํ–‰
}

 

โœ… ์˜ค๋Š˜์˜ ๋ฌธ์ œ : ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌํ•˜๊ธฐ

โšก ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ passwordText ๋ณ€์ˆ˜์— ์ €์žฅ

โšก passwordText ๋ฅผ ์ฒดํฌํ•˜์—ฌ ์›ํ•˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋งž์„ ๊ฒฝ์šฐ

Logcat์— “ํ†ต๊ณผ”๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ ๊ฒฝ์šฐ “ํ‹€๋ ธ์–ด์š”.“๋ฅผ ์ถœ๋ ฅ

 

โœ… [์„ ํƒ] ์˜ค๋Š˜์˜ ์‹ฌํ™” ๋ฌธ์ œ : ์•„์ด๋””(์ด๋ฉ”์ผ), ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฒดํฌํ•˜๊ธฐ

โšก ์ด๋ฉ”์ผ ์ž…๋ ฅ ์ฐฝ ์ถ”๊ฐ€

โšก ์ด๋ฉ”์ผ์ด ํ‹€๋ ธ์„ ๊ฒฝ์šฐ “์ด๋ฉ”์ผ์ด ํ‹€๋ ธ์–ด์š”” ์ถœ๋ ฅ

  • ์ด๋ฉ”์ผ์ด ๋งž์ง€๋งŒ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์„ ๋•Œ๋Š” “๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์–ด์š”.” ์ถœ๋ ฅ
  • ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋งž์„ ๊ฒฝ์šฐ “ํ†ต๊ณผ” ์ถœ๋ ฅ

๐Ÿ‘€ 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="40dp"
        android:text="์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
        app:layout_constraintHorizontal_bias="0.024"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="88dp"
        android:text="์ด๋ฉ”์ผ"
        app:layout_constraintHorizontal_bias="0.013"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="180dp"
        android:text="๋น„๋ฐ€๋ฒˆํ˜ธ"
        app:layout_constraintHorizontal_bias="0.013"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/editTextNumberPassword2"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:inputType="numberPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="21dp"
        android:layout_marginEnd="10dp"
        android:text="ํ™•์ธ"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextNumberPassword2" />

</androidx.constraintlayout.widget.ConstraintLayout>

๐Ÿ‘€ kt

package com.comu.android.secretmemo

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

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

        findViewById<Button>(R.id.button).setOnClickListener{
            // EditText ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐพ์•„์„œ editText ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
            val editEmail = findViewById<EditText>(R.id.editTextEmail)
            val emailText = editEmail.text.toString()

            // editText ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€ passwordText ์— ์ €์žฅํ•œ๋‹ค.
            val editText=findViewById<EditText>(R.id.editTextNumberPassword2)
            val pwText=editText.text.toString()

            if(emailText == "guswlsdl04@naver.com"){
                if(pwText == "123456"){
                    Log.d("SecretMemo", "ํ†ต๊ณผ")
                }
                else{
                    Log.d("SecretMemo","๋น„๋ฐ€๋ฒˆํ˜ธ ์˜ค๋ฅ˜")
                }
            }
            else Log.d("SecretMemo", "์ด๋ฉ”์ผ ์˜ค๋ฅ˜")
        }
    }
}

 

๐Ÿ‘€ ์‹คํ–‰๊ฒฐ๊ณผ 1

 

๐Ÿ‘€ ์‹คํ–‰๊ฒฐ๊ณผ 2

https://codemate.kr/@guswlsdl04/๋ชจ๊ฐ์ฝ”-์ฝ”ํ‹€๋ฆฐ-๊ธฐ์ดˆํŽธ-11์ผ์ฐจ โ—€ CLICK HERE!