λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
βœ’οΈ Android/Android Studio

[Android] 19. μœ„μ ― : μ—λ””νŠΈ ν…μŠ€νŠΈ(Edit Text) 와 λ²„νŠΌ

by A Lim Han 2023. 1. 10.

# μ—λ””νŠΈ ν…μŠ€νŠΈ(Edit Text) 와 λ²„νŠΌ

 

 

# μ—λ””νŠΈ ν…μŠ€νŠΈ(Edit Text) 와 λ²„νŠΌ

: μ‚¬μš©μžμ—κ²Œμ„œ νŠΉμ • 값을 μž…λ ₯λ°›κΈ° μœ„ν•œ 기본적인 μœ„μ ―μœΌλ‘œ, λͺ¨λ‘ View와 TextView 클래슀λ₯Ό μƒμ†λ°›λŠ”λ‹€.

 

 

- μ—λ””νŠΈ ν…μŠ€νŠΈ(Edit Text)

: 값을 μž…λ ₯받은 ν›„ ν•΄λ‹Ή 값을 Kotlin μ½”λ“œμ—μ„œ 가져와 μ‚¬μš©ν•˜λŠ” μš©λ„λ‘œ 이용

 

ex) 일반적인 μ—λ””νŠΈ ν…μŠ€νŠΈμ˜ XML μ½”λ“œ

<EditText
	android:id="@+id/edittext1" />

 

 

 

- μ—λ””νŠΈ ν…μŠ€νŠΈ(Edit Text)의 속성

β‘  hint 속성

: 클릭 μ‹œ μ‚¬λΌμ§€λŠ” 미리보기 μž‘μ„± κ°€λŠ₯

 

β‘‘ inputType 속성

: 속성값에 μž…λ ₯λ˜λŠ” μ˜΅μ…˜μ— 따라 ν‚€λ³΄λ“œμ˜ λͺ¨μ–‘ λ³€κ²½

 

+ μž…λ ₯ κ°€λŠ₯ν•œ μ˜΅μ…˜

[number] 숫자 ν˜•μ‹
[numberPassword] 숫자둜만 κ΅¬μ„±λœ λΉ„λ°€λ²ˆν˜Έ μž…λ ₯
[phone] μ „ν™”λ²ˆν˜Έ ν˜•μ‹
[textEmailAdress] 이메일 μ£Όμ†Œ ν˜•μ‹ 문자 μž…λ ₯
[date] λ‚ μ§œ ν˜•μ‹
[textUri] URI ν˜•μ‹μ˜ 문자 μž…λ ₯
[textPassword] λΉ„λ°€λ²ˆν˜Έ μž…λ ₯
[textVisiblePassword] λΉ„λ°€λ²ˆν˜Έλ₯Ό λ¬Έμžμ—΄ κ·ΈλŒ€λ‘œ ν‘œμ‹œ

 

 

 

- μ—λ””νŠΈ ν…μŠ€νŠΈμ™€ Kotlin μ½”λ“œ λ™μž‘

β‘  μ—λ””νŠΈ ν…μŠ€νŠΈ λ³€μˆ˜ μ„ μ–Έ

var myEdit : EditText

 

β‘‘ μƒμ„±ν•œ λ³€μˆ˜μ— μ—λ””νŠΈ ν…μŠ€νŠΈ μœ„μ ― λŒ€μž…

myEdit = findViewbyud<EditText>(R.id.edittext)

 

β‘’ μ—λ””νŠΈ ν…μŠ€νŠΈμ— μž…λ ₯된 κ°’ 뢈러였기

var myStr : String = myEdit.getText().toString()

 

 

 

- λ²„νŠΌ(Button)

-->  일반적인 λ²„νŠΌμ˜ XML μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™μŒ

<Button
	android:id="@+id/button1"
        android:text="확인" />

 

 

 

- λ²„νŠΌ(Button)κ³Ό Kotlin μ½”λ“œ λ™μž‘

β‘  λ²„νŠΌ λ³€μˆ˜ μ„ μ–Έ

var mybutton : Button

 

β‘‘ λ³€μˆ˜μ— λ²„νŠΌ μœ„μ ― λŒ€μž…

mybutton = findViewByid<Button>(R.id.button1)

 

β‘’ λ²„νŠΌ 클릭 μ‹œ λ™μž‘ν•˜λŠ” λžŒλ‹€μ‹ μ •μ˜

mybutton.setOnClickListener{ //λ™μž‘ λ‚΄μš© }

 

 

 

ex 1) λ²„νŠΌ 및 μ—λ””νŠΈ ν…μŠ€νŠΈ μ‹€μŠ΅ 1_μ „ν™”λ²ˆν˜Έ μž…λ ₯λ°›κΈ°

activity_main.xml

 

μ‹€ν–‰ ν™”λ©΄

 

 

 

ex 2) λ²„νŠΌ 및 μ—λ””νŠΈ ν…μŠ€νŠΈ μ‹€μŠ΅ 2_계산기 μ•± λ§Œλ“€κΈ°

 

β“˜ activity_main.xml νŒŒμΌμ„ μ•„λž˜μ™€ 같이 μˆ˜μ •

 

 

β‘‘ MainActivity.kt λ₯Ό μ•„λž˜μ™€ 같이 μˆ˜μ •

package com.example.calculatorapp

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

class MainActivity : AppCompatActivity() {

    lateinit var edit1 : EditText
    lateinit var edit2 : EditText
    lateinit var btnAdd : Button
    lateinit var btnSub : Button
    lateinit var btnMul : Button
    lateinit var btnDiv : Button
    lateinit var textResult : TextView

    lateinit var num1 : String
    lateinit var num2 : String

    var result : Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "계산기"

        edit1 = findViewById(R.id.Edit1)
        edit2 = findViewById(R.id.Edit2)
        btnAdd = findViewById(R.id.BtnAdd)
        btnSub = findViewById(R.id.BtnSub)
        btnMul = findViewById(R.id.BtnMul)
        btnDiv = findViewById(R.id.BtnDiv)
        textResult = findViewById(R.id.TextResult)

        btnAdd.setOnTouchListener { v, event ->
            num1 = edit1.text.toString()
            num2 = edit2.text.toString()
            result = Integer.parseInt(num1)+Integer.parseInt(num2)
            textResult.text = "계산 κ²°κ³Ό : " + result.toString()
            false
        }

        btnSub.setOnTouchListener { v, event ->
            num1 = edit1.text.toString()
            num2 = edit2.text.toString()
            result = Integer.parseInt(num1)-Integer.parseInt(num2)
            textResult.text = "계산 κ²°κ³Ό : " + result.toString()
            false
        }

        btnMul.setOnTouchListener { v, event ->
            num1 = edit1.text.toString()
            num2 = edit2.text.toString()
            result = Integer.parseInt(num1)*Integer.parseInt(num2)
            textResult.text = "계산 κ²°κ³Ό : " + result.toString()
            false
        }

        btnDiv.setOnTouchListener { v, event ->
            num1 = edit1.text.toString()
            num2 = edit2.text.toString()
            result = Integer.parseInt(num1)/Integer.parseInt(num2)
            textResult.text = "계산 κ²°κ³Ό : " + result.toString()
            false
        }
    }
}

 

 

β‘’ App을 μ‹€ν–‰  -->  μ •μƒμ μœΌλ‘œ 싀행됨을 확인