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

Study/Android

[์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ] 7. ์œ„์ ฏ - ํ…์ŠคํŠธ๋ทฐ/์—๋””ํŠธํ…์ŠคํŠธ,๋ฒ„ํŠผ/์ปดํŒŒ์šด๋“œ๋ฒ„ํŠผ/์ด๋ฏธ์ง€๋ฒ„ํŠผ

๋“ค์–ด๊ฐ€๊ธฐ ์ „..

[์ฐธ๊ณ ] XML

์—˜๋ฆฌ๋จผํŠธ

- root element๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ child element๋ฅผ ๊ฐ€์ง

- ๊ฐ child element๋„ ํ•˜๋‚˜ ์ด์ƒ์˜ subchild element๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ

<root element>
    <child element>
        <subchild element>๋ฐ์ดํ„ฐ</subchild element>
    </child element>
</element>

- ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ์—˜๋ฆฌ๋จผํŠธ๋Š” empty element๋ผ๊ณ  ํ•˜๋ฉฐ, <element></element>๋˜๋Š” <element/>๋กœ ํ‘œํ˜„ํ•จ

- ์—˜๋ฆฌ๋จผํŠธ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฐ ์†์„ฑ์€ ์†์„ฑ๋ช…๊ณผ ์†์„ฑ๊ฐ’์œผ๋กœ ํ‘œํ˜„

<element ์†์„ฑ๋ช…="์†์„ฑ๊ฐ’">

 

์ฃผ์„๋ฌธ

- xml๋ฌธ์„œ์˜ ์ฃผ์„๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•จ

<!-- ์ฃผ์„๋ฌธ -->

์œ„์ ฏ

 

ํ…์ŠคํŠธ๋ทฐ

- View ํด๋ž˜์Šค ๋ฐ”๋กœ ๋‹ค์Œ์— ์œ„์น˜ํ•˜๋ฉฐ ๋‹ค์–‘ํ•œ ์œ„์ ฏ์ด ๊ทธ ํ•˜์œ„์— ์กด์žฌํ•จ

 

text ์†์„ฑ

- ํ…์ŠคํŠธ๋ทฐ์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž์—ด์„ ํ‘œํ˜„

- “๋ฌธ์ž์—ด” ํ˜•์‹์œผ๋กœ ๊ฐ’์„ ์ง์ ‘ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ “@string/๋ณ€์ˆ˜๋ช…” ํ˜•์‹์œผ๋กœ ์ง€์ •ํ•œ ํ›„ strings.xml ํŒŒ์ผ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

 

textColor ์†์„ฑ

- ๊ธ€์ž์˜ ์ƒ‰์ƒ์„ ์ง€์ •

- background ์†์„ฑ์ฒ˜๋Ÿผ ๊ฐ’์€ #RRGGBB๋‚˜ #AARRGGBB ํ˜•์‹์„ ์‚ฌ์šฉํ•จ

 

textSize ์†์„ฑ

- ๊ธ€์ž์˜ ํฌ๊ธฐ๋ฅผ dp, px, in, mm, sp ๋‹จ์œ„๋กœ ์ง€์ •

- ํ…์ŠคํŠธ๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ์œ„์ ฏ(ํ…์ŠคํŠธ๋ทฐ, ์—๋””ํŠธํ…์ŠคํŠธ)์€ ์ฃผ๋กœ sp๋ฅผ ์‚ฌ์šฉ. sp(๋˜๋Š” sip)๋Š” Scale-independent Pixels์˜ ์•ฝ์ž๋กœ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋‹จ์œ„

- (์œ„์ ฏ์— ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š๊ณ  ๊ธ€์ž๋งŒ ๋ณ€๊ฒฝ๋˜๋ฏ€๋กœ sp ์‚ฌ์šฉ ๊ถŒ์žฅ)

 

typeface ์†์„ฑ

- ๊ธ€์ž์˜ ๊ธ€๊ผด์„ ์ง€์ •

- ๊ฐ’์œผ๋กœ sans, serif, monospace๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ๋””ํดํŠธ๋Š” normal

 

textStyle ์†์„ฑ

- ๊ธ€์ž์˜ ์Šคํƒ€์ผ์„ ์ง€์ •

- ๊ฐ’์œผ๋กœ bold, italic, bold|italic์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ๋””ํดํŠธ๋Š” normal

 

singleLine ์†์„ฑ

- ๊ธ€์ด ๊ธธ์–ด ์ค„์ด ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ ๊ฐ•์ œ๋กœ ํ•œ ์ค„๊นŒ์ง€๋งŒ ์ถœ๋ ฅํ•˜๊ณ  ๋ฌธ์ž์—ด์˜ ๋งจ ๋’ค์— ‘...’๋ฅผ ํ‘œ์‹œ

- ๊ฐ’์œผ๋กœ true์™€ false๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ๋””ํดํŠธ๋Š” false

 

maxLines ์†์„ฑ

- ํ…์ŠคํŠธ๋ทฐ์— ์ตœ๋Œ€ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ ์ค„ ์ˆ˜๋ฅผ ์„ค์ •

 

minLines ์†์„ฑ

- ๋ฐ˜๋Œ€๋กœ ์ตœ์†Œ ์ค„ ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •

 

ellipsize ์†์„ฑ

- ํ…์ŠคํŠธ๋ทฐ์˜ maxLines ์†์„ฑ์ด 1์ด๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์ด ๊ธธ์–ด์„œ ๊ธ€์ž๊ฐ€ ์ž˜๋ฆด ๋•Œ์— ์„ค์ •

 

lines ์†์„ฑ

- ํ…์ŠคํŠธ๋ทฐ ๋†’์ด ๊ณ ์ •ํ•˜๊ธฐ

 

maxLength

- ํ…์ŠคํŠธ ์ „์ฒด ๊ธธ์ด ์ œํ•œํ•˜๊ธฐ

- maxLength๋Š” ํ…์ŠคํŠธ์˜ ์ „์ฒด ๊ธธ์ด๋ฅผ ์ œํ•œ, ์„ค์ •ํ•œ ๊ฐ’๋งŒํผ์˜ ๊ธ€์ž๋งŒ ๋ณด์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ณด์ด์ง€ ์•Š๊ฒŒ ํ•จ

    <TextView
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="10"
        android:maxLength="10"
        android:maxLines="1"
        android:singleLine="false"
        android:text="TextView ํ…์ŠคํŠธ๋ทฐTextView ํ…์ŠคํŠธ๋ทฐTextView ํ…์ŠคํŠธ๋ทฐTextView ํ…์ŠคํŠธ๋ทฐTextView ํ…์ŠคํŠธ๋ทฐ"
        android:textColor="#0000FF"
        android:textSize="30sp"
        android:textStyle="bold|italic"
        android:typeface="serif"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

 

๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” View ํด๋ž˜์Šค ๋˜๋Š” TextView ํด๋ž˜์Šค์˜ XML ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ

 

์‹ค์Šต (xml ์ฝ”๋“œ ์ƒ๋žต)

package com.example.textview

import android.graphics.Color
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

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

        var tv1: TextView
        var tv2: TextView
        var tv3: TextView

        tv1 = findViewById(R.id.textView1)
        tv2 = findViewById(R.id.textView2)
        tv3 = findViewById(R.id.textView3)

        tv1.setText("์•ˆ๋…•ํ•˜์„ธ์š”?")
        tv1.setTextColor(Color.RED)
        tv2.setTextSize(30.0f)
        tv2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC)
        tv3.text = "๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ๋ฐ”์‚ฌ์•„์ž์ฐจ์นดํƒ€ํŒŒํ•˜๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ๋ฐ”์‚ฌ์•„์ž์ฐจ์นดํƒ€ํŒŒํ•˜๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ๋ฐ”์‚ฌ์•„์ž์ฐจ์นดํƒ€ํŒŒํ•˜"
        tv3.setSingleLine()
    }
}

 

 


์—๋””ํŠธํ…์ŠคํŠธ, ๋ฒ„ํŠผ

- ์‚ฌ์šฉ์ž์—๊ฒŒ์„œ ์–ด๋–ค ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์œ„์ ฏ

- View์™€ TextView ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Œ ⇒ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

<TextView
	android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ํ…์ŠคํŠธ๋ทฐ" />
    
<Button
	android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="๋ฒ„ํŠผ" />

[์ฐธ๊ณ ]

TextView https://developer.android.com/reference/kotlin/android/widget/TextView

EditText https://developer.android.com/reference/kotlin/android/widget/EditTextrs

Button https://developer.android.com/reference/kotlin/android/widget/Button

 

์—๋””ํŠธํ…์ŠคํŠธ

- ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์€ ํ›„ ํ•ด๋‹น ๊ฐ’์„ Kotlin ์ฝ”๋“œ์—์„œ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๋Š” ์šฉ๋„๋กœ ๋งŽ์ด ์“ฐ์ž„

- ์ผ๋ฐ˜์ ์ธ ์—๋””ํŠธํ…์ŠคํŠธ์˜ XML ์ฝ”๋“œ

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

 

hint ์†์„ฑ

- ํด๋ฆญํ•˜๋ฉด ์‚ฌ๋ผ์ง€๋Š” ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ

 

inputType ์†์„ฑ

- inputType ์†์„ฑ์— ์ž…๋ ฅ๋˜๋Š” ์˜ต์…˜์— ๋”ฐ๋ผ ํ‚ค๋ณด๋“œ์˜ ๋ชจ์–‘์ด ๋ฐ”๋€œ

    - [number] : ์ˆซ์ž ํ˜•์‹

    - [numberPassword] : ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ

    - [phone] : ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹

    - [date] : ๋‚ ์งœ ํ˜•์‹

    - [textUri] : URI ํ˜•์‹์˜ ๋ฌธ์ž ์ž…๋ ฅ 

    - [textEmailAdress] : email ์ฃผ์†Œ ํ˜•์‹์˜ ๋ฌธ์ž ์ž…๋ ฅ 

    - [textPassword]: ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ

    - [textVisiblePassword] : ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ํ‘œ์‹œํ•˜๊ธฐ

 

๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” Kotlin ์ฝ”๋“œ๋ฅผ ์„ธ ๋‹จ๊ณ„๋กœ ์ž‘์„ฑ

- ์—๋””ํŠธํ…์ŠคํŠธ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ด ๋ณ€์ˆ˜์— ํ•ด๋‹น id ๊ฐ’์„ ๋„ฃ์€ ํ›„์— ์ ‘๊ทผํ•จ

1. ์—๋””ํŠธํ…์ŠคํŠธ ๋ณ€์ˆ˜ ์„ ์–ธ

    - var myEdit : EditText 

2. ๋ณ€์ˆ˜์— ์—๋””ํŠธํ…์ŠคํŠธ ์œ„์ ฏ ๋Œ€์ž…

    - myEdit = findViewById(R.id.edittext)

3. ์—๋””ํŠธํ…์ŠคํŠธ์— ์ž…๋ ฅ๋œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ → ์ฃผ๋กœ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ๋žŒ๋‹ค์‹ ์•ˆ์— ๋„ฃ์Œ

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

 

๋ฒ„ํŠผ

- ์ผ๋ฐ˜์ ์ธ ๋ฒ„ํŠผ์˜ XML ์ฝ”๋“œ

<Button
    android:id="@+id/button1"
    android:text="ํ™•์ธ" />

 

๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” Kotlin ์ฝ”๋“œ๋ฅผ ์„ธ ๋‹จ๊ณ„๋กœ ์ž‘์„ฑ

1. ๋ฒ„ํŠผ ๋ณ€์ˆ˜ ์„ ์–ธ

    - var mybutton : Button

2. ๋ณ€์ˆ˜์— ๋ฒ„ํŠผ ์œ„์ ฏ ๋Œ€์ž…

    - mybutton = findViewById(R.id.button1)

3. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋žŒ๋‹ค์‹ ์ •์˜

    - mybutton.setOnClickListener { // ๋™์ž‘ ๋‚ด์šฉ์„ ์ด ๋ถ€๋ถ„์— }

→ ์„ธ ๋‹จ๊ณ„๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์œ„์ ฏ(๋ผ๋””์˜ค๋ฒ„ํŠผ, ์ด๋ฏธ์ง€๋ฒ„ํŠผ, ์ฒดํฌ๋ฐ•์Šค, ํ† ๊ธ€๋ฒ„ํŠผ ๋“ฑ)์—์„œ ๊ฑฐ์˜ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋จ

 

์‹ค์Šต1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="์ „ํ™”๋ฒˆํ˜ธ" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="000-0000-0000"
            android:inputType="phone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="์ž…๋ ฅ" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="์ทจ์†Œ" />

    </LinearLayout>

</LinearLayout>

 

 

์‹ค์Šต2 - ์ดˆ๊ฐ„๋‹จ ๊ณ„์‚ฐ๊ธฐ ์•ฑ ๋งŒ๋“ค๊ธฐ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/Edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="์ˆซ์ž1"/>

    <EditText
        android:id="@+id/Edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="์ˆซ์ž2"/>

    <Button
        android:id="@+id/BtnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋”ํ•˜๊ธฐ"/>

    <Button
        android:id="@+id/BtnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋นผ๊ธฐ"/>

    <Button
        android:id="@+id/BtnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๊ณฒํ•˜๊ธฐ"/>

    <Button
        android:id="@+id/BtnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋‚˜๋ˆ„๊ธฐ"/>

    <TextView
        android:id="@+id/TextResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textColor="#ff0000"
        android:textSize="30sp"
        android:text="๊ณ„์‚ฐ ๊ฒฐ๊ณผ : "/>

</LinearLayout>

 

package com.example.simplecalapp

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
        }
    }
}

 

 


์ปดํŒŒ์šด๋“œ๋ฒ„ํŠผ

CompoundButton ํด๋ž˜์Šค 

- Button ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ์ฒดํฌ๋ฐ•์Šค, ๋ผ๋””์˜ค๋ฒ„ํŠผ, ์Šค์œ„์น˜, ํ† ๊ธ€๋ฒ„ํŠผ์˜ ์ƒ์œ„ ํด๋ž˜์Šค

- ์ด ๋„ค ๊ฐ€์ง€๋Š” ๊ณตํ†ต์ ์œผ๋กœ ์ฒดํฌ ๋˜๋Š” ์–ธ์ฒดํฌ ์ƒํƒœ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ 

    - ์‹ค์ œ๋กœ ๋น„์Šทํ•œ ํ˜•ํƒœ๋ฅผ ๋ ์ง€๋งŒ ์šฉ๋„๋Š” ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ„

 

์ฒดํฌ๋ฐ•์Šค

- ์ฒดํฌ๋ฐ•์Šค๋Š” ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ์ƒํƒœ๊ฐ€ ์ฒดํฌ, ์–ธ์ฒดํฌ๋กœ ๋ฐ”๋€œ

- ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์žˆ์–ด๋„ ์„œ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์–ด ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋™์‹œ์— ์ฒดํฌํ•  ์ˆ˜ ์žˆ์Œ

 

์ฒดํฌ์™€ ์–ธ์ฒดํฌ๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ Kotlin ์ฝ”๋“œ์˜ ์ฒ˜๋ฆฌ ์ ˆ์ฐจ

1. ์ฒดํฌ๋ฐ•์Šค ๋ณ€์ˆ˜ ์„ ์–ธ

    - var mycheck : CheckBox

2. ๋ณ€์ˆ˜์— ์ฒดํฌ๋ฐ•์Šค ์œ„์ ฏ ๋Œ€์ž…

    - mycheck = findViewById(R.id.android) 

3. ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋žŒ๋‹ค์‹ ์ •์˜

    - mycheck.setOnCheckedChangeListener { compoundButton, b -> // ๋™์ž‘ ๋‚ด์šฉ์„ ์ด ๋ถ€๋ถ„์— ์ฝ”๋”ฉ }

 

์‹ค์Šต 

๋”๋ณด๊ธฐ

[์ฐธ๊ณ ] ํ† ์ŠคํŠธ

- ํ™”๋ฉด ํ•˜๋‹จ๋ถ€์— ๋ฉ”์‹œ์ง€๋ฅผ ์ž ์‹œ๋™์•ˆ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ์‹

- Toast.makeText(Context context, String message, int duration).show()

package com.example.checkbox

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    lateinit var checkApple : CheckBox
    lateinit var checkOrange : CheckBox
    lateinit var checkBanana : CheckBox

    var listener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
        if(isChecked){
            when(buttonView.id)
            {
                R.id.checkApple -> Toast.makeText(applicationContext,"์‚ฌ๊ณผ", Toast.LENGTH_SHORT).show()
                R.id.checkOrange -> Toast.makeText(applicationContext, "์˜ค๋ Œ์ง€", Toast.LENGTH_SHORT).show()
                R.id.checkBanana -> Toast.makeText(applicationContext, "๋ฐ”๋‚˜๋‚˜", Toast.LENGTH_SHORT).show()
            }
        }
    }

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

        checkApple = findViewById(R.id.checkApple)
        checkOrange = findViewById(R.id.checkOrange)
        checkBanana = findViewById(R.id.checkBanana)

        checkApple.setOnCheckedChangeListener(listener)
        checkOrange.setOnCheckedChangeListener(listener)
        checkBanana.setOnCheckedChangeListener(listener)

//        checkApple.setOnCheckedChangeListener { buttonView, isChecked ->
//            if(isChecked) {
//                Toast.makeText(applicationContext, "์‚ฌ๊ณผ", Toast.LENGTH_SHORT).show()
//            }
//        }
//
//        checkOrange.setOnCheckedChangeListener { buttonView, isChecked ->
//            if(isChecked) {
//                Toast.makeText(applicationContext, "์˜ค๋ Œ์ง€", Toast.LENGTH_SHORT).show()
//            }
//        }
//
//        checkBanana.setOnCheckedChangeListener { buttonView, isChecked ->
//            if(isChecked) {
//                Toast.makeText(applicationContext, "๋ฐ”๋‚˜๋‚˜", Toast.LENGTH_SHORT).show()
//            }
//        }
    }
}

 

 

์Šค์œ„์น˜, ํ† ๊ธ€๋ฒ„ํŠผ

- ์Šค์œ„์น˜์™€ ํ† ๊ธ€๋ฒ„ํŠผ์€ ๋ชจ์–‘๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฅผ ๋ฟ ์šฉ๋„๋Š” ๊ฑฐ์˜ ๋™์ผํ•จ

- ์Šค์œ„์น˜์˜ ์ฃผ ์šฉ๋„๋Š” ์˜จ/์˜คํ”„ ์ƒํƒœ ํ‘œ์‹œ

    - XML ์†์„ฑ์ด๋‚˜ ๊ด€๋ จ ๋ฉ”์†Œ๋“œ๋Š” ๋ชจ๋‘ ์ฒดํฌ๋ฐ•์Šค์™€ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

    - checked ์†์„ฑ์€ true์™€ false์— ๋”ฐ๋ผ์„œ ์ƒ‰์ƒ๊ณผ ๊ธ€์ž๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„

 

๋ผ๋””์˜ค๋ฒ„ํŠผ, ๋ผ๋””์˜ค๊ทธ๋ฃน

๋ผ๋””์˜ค๋ฒ„ํŠผ

- XML ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฒดํฌ๋ฐ•์Šค์™€ ๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ ์šฉ๋„๊ฐ€ ๋‹ค๋ฆ„

- ์—ฌ๋Ÿฌ ๊ฐœ ์ค‘ ํ•˜๋‚˜๋งŒ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ

    » ๊ทธ๋Ÿฌ๋‚˜ ๋ผ๋””์˜ค๋ฒ„ํŠผ๋งŒ ์—ฌ๋Ÿฌ ๊ฐœ ๋‚˜์—ดํ•˜๋ฉด ํด๋ฆญํ•˜๋Š” ๊ฒƒ๋งˆ๋‹ค ๋ชจ๋‘ ์ค‘๋ณต ์„ ํƒ๋˜๋ฏ€๋กœ ๋ผ๋””์˜ค๊ทธ๋ฃน๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

    » ๊ฐ ๋ผ๋””์˜ค๋ฒ„ํŠผ์˜ id ์†์„ฑ์ด ๊ผญ ์žˆ์–ด์•ผ ํ•จ

        » id ์†์„ฑ์ด ์—†์œผ๋ฉด ํ•ด๋‹น ๋ผ๋””์˜ค๋ฒ„ํŠผ์ด ๊ณ„์† ์„ ํƒ๋œ ๊ฒƒ์œผ๋กœ ์ง€์ •๋˜์–ด ํ•ด์ œ๋˜์ง€ ์•Š์Œ

 

๋ผ๋””์˜ค๊ทธ๋ฃน

- ViewGroup-LinearLayout์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ์กด์žฌํ•จ

- TextView ํ•˜์œ„์˜ ์œ„์ ฏ๋“ค๊ณผ๋Š” ์„ฑ๊ฒฉ์ด ์กฐ๊ธˆ ๋‹ค๋ฆ„

- ๋ผ๋””์˜ค๊ทธ๋ฃน์—์„œ ๊ฐ€๋” ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ธ clearCheck()

    » ํ•ด๋‹น ๋ผ๋””์˜ค๊ทธ๋ฃน ์•ˆ์— ์ฒดํฌ๋œ ๊ฒƒ์„ ๋ชจ๋‘ ํ•ด์ œํ•จ

 

์‹ค์Šต

 

package com.example.radiogroup

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    lateinit var radioGroup : RadioGroup
    lateinit var radioApple : RadioButton
    lateinit var radioOrange : RadioButton
    lateinit var radioBanana : RadioButton

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

        radioGroup = findViewById(R.id.radioGroup)
        radioApple = findViewById(R.id.radioApple)
        radioOrange = findViewById(R.id.radioOrange)
        radioBanana = findViewById(R.id.radioBanana)

        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId) {
                R.id.radioApple -> Toast.makeText(applicationContext, "์‚ฌ๊ณผ", Toast.LENGTH_SHORT).show()
                R.id.radioOrange -> Toast.makeText(applicationContext, "์˜ค๋ Œ์ง€", Toast.LENGTH_SHORT).show()
                R.id.radioBanana -> Toast.makeText(applicationContext, "๋ฐ”๋‚˜๋‚˜", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

 

 


์ด๋ฏธ์ง€๋ฒ„ํŠผ

์ด๋ฏธ์ง€๋ทฐ

- ๊ทธ๋ฆผ์„ ์ถœ๋ ฅํ•˜๋Š” ์œ„์ ฏ 

- ๊ทธ๋ฆผ์„ ๋„ฃ๊ฑฐ๋‚˜ ํ™”๋ฉด์„ ํ™”๋ คํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•จ

    - ์ด๋ฏธ์ง€๋ทฐ์— ๋ณด์—ฌ์ค„ ๊ทธ๋ฆผ ํŒŒ์ผ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋กœ์ ํŠธ์˜ [res]-[drawable] ํด๋”์— ์žˆ์–ด์•ผ ํ•จ

    - ์ ‘๊ทผ์€ XML์—์„œ “@drawable/๊ทธ๋ฆผ ์•„์ด๋””” ํ˜•์‹์œผ๋กœ ํ•จ

 

์ด๋ฏธ์ง€๋ทฐ์™€ ์ด๋ฏธ์ง€๋ฒ„ํŠผ์˜ XML ์†์„ฑ

- src

    - ์ด๋ฏธ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋ƒ„

- maxHeight/maxWidth

    - ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•จ

- scaleType

    - ์ด๋ฏธ์ง€์˜ ํ™•๋Œ€/์ถ•์†Œ ๋ฐฉ์‹์„ ์ง€์ • 

    - scaleType์˜ ์†์„ฑ: matrix, fitXY, fitStart, fitEnd, center ๋“ฑ ์ง€์ •ํ•œ ๊ฐ’์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€๋ฅผ ํ™•๋Œ€/์ถ•์†Œํ•˜๋Š” ๋ฐฉ์‹์ด ๊ฒฐ์ •๋จ

        - matrix: ์‹ค์ œ ์ด๋ฏธ์ง€๋ฅผ ์ขŒ์ธก ์ƒ๋‹จ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฒ„ํŠผ์˜ ํฌ๊ธฐ๋งŒํผ ๋ณด์—ฌ์คŒ

        - fitXY: ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค์„œ ๋งž์ถฐ์„œ ๋ณด์—ฌ์คŒ

        - fitStart: ์ขŒ์ธก ์ƒ๋‹จ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ด๋ฏธ์ง€ ๋น„์œจ์— ๋งž๊ฒŒ ์กฐ์ •ํ•ด์„œ ์œ„์ ฏ ์•ˆ์— ๋„ฃ์–ด์คŒ

        - fitEnd: ์šฐ์ธก ํ•˜๋‹จ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ด๋ฏธ์ง€ ๋น„์œจ์— ๋งž๊ฒŒ ์กฐ์ •ํ•ด์„œ ์œ„์ ฏ ์•ˆ์— ๋„ฃ์–ด์คŒ

        - cemter: ์‹ค์ œ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ๊ทธ๋Œ€๋กœ ์ •์ค‘์•™์— ์œ„์น˜ ์‹œํ‚ด(์œ„์ ฏ๋ณด๋‹ค ์ด๋ฏธ์ง€ ํด ๊ฒฝ์šฐ ์ž˜๋ ค ๋ณด์ผ ์ˆ˜ ์žˆ์Œ)

 

์ด๋ฏธ์ง€๋ทฐ์™€ ์ด๋ฏธ์ง€๋ฒ„ํŠผ์˜ XML ์†์„ฑ

- ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ทธ๋ฆผ ํŒŒ์ผ์ด [res]-[drawable] ํด๋”์— ์žˆ์–ด์•ผ ํ•จ

- ํŒŒ์ผ ํฌ๋งท

    - png, jpg, gif๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ png๋‚˜ jpg๋ฅผ ๊ถŒ์žฅํ•จ

- [res]-[mipmap]

    - ๊ฐ™์€ ์ด๋ฆ„์˜ ์•ฑ ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€(ic_launcher.png)๊ฐ€ ๋””ํดํŠธ๋กœ ๋“ค์–ด ์žˆ์Œ

    - ๊ฐ๊ฐ์€ ๊ฐ™์€ ์ด๋ฏธ์ง€์ด์ง€๋งŒ ํ•ด์ƒ๋„๊ฐ€ mdpi(48×48), hdpi(72×72), xhdpi(96×96), xxhdpi(144×144), xxxhdpi(192× 192) ๋“ฑ์œผ๋กœ ๋‹ค๋ฆ„

    - XML ํŒŒ์ผ์—์„œ๋Š” @mipmap/ic_launcher๋กœ, Kotlin ์ฝ”๋“œ์—์„œ๋Š” R.mipmap.ic_launcher๋กœ ์‚ฌ์šฉ๋จ

    - ์ด ์•ฑ ์•„์ด์ฝ˜์ธ ic_launcher๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ์šด์˜์ฒด์ œ๊ฐ€ ํ˜„์žฌ ์•ˆ๋“œ๋กœ์ด๋“œํฐ์˜ ํ•ด์ƒ๋„์— ์ ์ ˆํ•œ ์ด๋ฏธ์ง€๋ฅผ ์•Œ์•„์„œ ์„ ํƒํ•จ

 

์˜ˆ์ œ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/q10" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/q10" />
    </LinearLayout>

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="fitCenter" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="fitXY" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="fitStart" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="fitEnd" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="center" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:background="#ffff00"
        android:src="@drawable/q10"
        android:scaleType="matrix" />

</LinearLayout>

 

 


์‹ค์Šต - ๋ฐ˜๋ ค๋™๋ฌผ ์‚ฌ์ง„ ๋ณด๊ธฐ ์•ฑ ๋งŒ๋“ค๊ธฐ

๋”๋ณด๊ธฐ

์•ก์…˜๋ฐ”(Action bar)๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์—…๋„ค๋น„๊ฒŒ์ด์…˜ ์‹ค์Šต ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์—†๊ธฐ๋•Œ๋ฌธ์— themes.xml์˜ ์†์„ฑ ์ค‘ <style name = "Base.Theme.ProjectBMICalculator" parent="Theme.Material3.Light.NoActionBar">์˜ NoActionBar๋ฅผ ์ง€์›Œ์ค๋‹ˆ๋‹ค. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์„ ํƒ์„ ์‹œ์ž‘ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/ChkAgree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์‹œ์ž‘ํ•จ" />

    <TextView
        android:id="@+id/Text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์ข‹์•„ํ•˜๋Š” ๋ฐ˜๋ ค๋™๋ฌผ์€?"
        android:textSize="20sp"
        android:visibility="invisible" />

    <RadioGroup
        android:id="@+id/Rgroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible">

        <RadioButton
            android:id="@+id/RdoDog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="๊ฐ•์•„์ง€" />

        <RadioButton
            android:id="@+id/RdoCat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="๊ณ ์–‘์ด" />

        <RadioButton
            android:id="@+id/RdoRabbit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ํ† ๋ผ" />

    </RadioGroup>

    <Button
        android:id="@+id/BtnOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์„ ํƒ์™„๋ฃŒ"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/ImgPet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible" />

</LinearLayout>

 

package com.example.petimageapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    lateinit var text1 : TextView
    lateinit var text2 : TextView
    lateinit var chkAgree : CheckBox
    lateinit var rGroup1 : RadioGroup
    lateinit var rdoDog : RadioButton
    lateinit var rdoCat : RadioButton
    lateinit var rdoRabbit : RadioButton
    lateinit var btnOk : Button
    lateinit var imgPet : ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "๋ฐ˜๋ ค๋™๋ฌผ ์‚ฌ์ง„ ๋ณด๊ธฐ"

        text1 = findViewById(R.id.Text1)
        chkAgree = findViewById(R.id.ChkAgree)

        text2 = findViewById(R.id.Text2)
        rGroup1 = findViewById(R.id.Rgroup1)
        rdoDog = findViewById(R.id.RdoDog)
        rdoCat = findViewById(R.id.RdoCat)
        rdoRabbit = findViewById(R.id.RdoRabbit)

        btnOk = findViewById(R.id.BtnOk)
        imgPet = findViewById(R.id.ImgPet)

        chkAgree.setOnCheckedChangeListener { buttonView, isChecked ->
            if(chkAgree.isChecked == true) {
                text2.visibility = android.view.View.VISIBLE
                rGroup1.visibility = android.view.View.VISIBLE
                btnOk.visibility = android.view.View.VISIBLE
                imgPet.visibility = android.view.View.VISIBLE
            }
            else {
                text2.visibility = android.view.View.INVISIBLE
                rGroup1.visibility = android.view.View.INVISIBLE
                btnOk.visibility = android.view.View.INVISIBLE
                imgPet.visibility = android.view.View.INVISIBLE
            }
        }

        btnOk.setOnClickListener {
            when(rGroup1.checkedRadioButtonId) {
                R.id.RdoDog -> imgPet.setImageResource(R.drawable.dog)
                R.id.RdoCat -> imgPet.setImageResource(R.drawable.cat)
                R.id.RdoRabbit -> imgPet.setImageResource(R.drawable.rabbit)
                else -> Toast.makeText(applicationContext, "๋™๋ฌผ ๋จผ์ € ์„ ํƒํ•˜์„ธ์š”", Toast.LENGTH_SHORT).show()
            }
        }
    }
}