๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android]์•ˆ๋“œ๋กœ์ด๋“œ - ์ฒดํฌ๋ฐ•์Šค ํด๋ฆญ ์‹œ ๋‹ค๋ฅธ ์ฒดํฌ๋ฐ•์Šค ์ „์ฒด ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

๐Ÿค–Android

[Android]์•ˆ๋“œ๋กœ์ด๋“œ - ์ฒดํฌ๋ฐ•์Šค ํด๋ฆญ ์‹œ ๋‹ค๋ฅธ ์ฒดํฌ๋ฐ•์Šค ์ „์ฒด ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 2. 1. 17:59
๋ฐ˜์‘ํ˜•

 

ํšŒ์›๊ฐ€์ž… ์‹œ ์ด์šฉ์•ฝ๊ด€ ๋™์˜/๋งˆ์ผ€ํŒ… ๋™์˜๋ฅผ ์ฒดํฌ๋ฐ•์Šค๋กœ ํ•  ๋•Œ
ํ•ญ์ƒ ์ „์ฒด ์„ ํƒ ๋˜๋Š” ์ „์ฒด ๋™์˜ ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์žˆ๋‹ค.
๊ทธ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ „์ฒด ์„ ํƒ๋˜๋Š” ์ฒดํฌ๋ฐ•์Šค ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

 

 

1. main.xml

๋จผ์ € ์ฒดํฌ๋ฐ•์Šค๋“ค์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
์ „์ฒด ์„ ํƒ ์ฒดํฌ๋ฐ•์Šค์™€ 4๊ฐœ์˜ ์ฒดํฌ๋ฐ•์Šค๋ฅผ LinearLayout ์— ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•ด์ค€๋‹ค.

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

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

        <CheckBox
            android:id="@+id/ckbAllAgree"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:text="์ „์ฒด๋™์˜" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:text="์ด์šฉ์•ฝ๊ด€(ํ•„์ˆ˜)" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:text="๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ๋ฐ ์ด์šฉ๋™์˜(ํ•„์ˆ˜)" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:text="๋งŒ 14์„ธ ์ด์ƒ์ž…๋‹ˆ๋‹ค.(ํ•„์ˆ˜)" />

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:text="์ด๋ฒคํŠธ, ์‡ผํ•‘ ํ”„๋กœ๋ชจ์…˜ ๋ฉ”์ผ ์ˆ˜์‹  ๋™์˜(์„ ํƒ)" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

2. MainActivity.java

๊ทธ๋ฆฌ๊ณ  ๋ฉ”์ธ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ์ฒดํฌ๋ฐ•์Šค ํ™”๋ฉด์—ฐ๊ฒฐ์„ ํ•ด์ฃผ๊ณ 

๋งจ์ฒ˜์Œ ์ฒดํฌ๋ฐ•์Šค์— setOnClickListener๋กœ
์ฒซ ์ฒดํฌ๋ฐ•์Šค ํด๋ฆญ์‹œ ๋‚˜๋จธ์ง€ ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์ „๋ถ€ ์ฒดํฌ๊ฐ€ ๋˜๊ณ 
์ฒซ ์ฒดํฌ๋ฐ•์Šค ์ฒดํฌ ํ•ด์ œ์‹œ ๋‚˜๋จธ์ง€ ์ฒดํฌ๋ฐ•์Šค๋„ ์ „๋ถ€ ์ฒดํฌ ํ•ด์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ์„ค์ •ํ•ด์ค€๋‹ค.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
    CheckBox ckbAllAgree;
    CheckBox checkBox1;
    CheckBox checkBox2;
    CheckBox checkBox3;
    CheckBox checkBox4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ckbAllAgree = findViewById(R.id.ckbAllAgree);
        checkBox1 = findViewById(R.id.checkBox1);
        checkBox2 = findViewById(R.id.checkBox2);
        checkBox3 = findViewById(R.id.checkBox3);
        checkBox4 = findViewById(R.id.checkBox4);

        ckbAllAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ckbAllAgree.isChecked();
                checkBox1.setChecked(isChecked);
                checkBox2.setChecked(isChecked);
                checkBox3.setChecked(isChecked);
                checkBox4.setChecked(isChecked);
            }
        });
    }
}

 

3. ์‹คํ–‰ํ™”๋ฉด

์™ผ์ชฝ์€ ์ฒดํฌ๋ฐ•์Šค ํ•ด์ œํ–ˆ์„ ๋•Œ

์˜ค๋ฅธ์ชฝ์€ ์ฒดํฌ๋ฐ•์Šค ์„ ํƒํ–ˆ์„ ๋•Œ์ด๋‹ค.

 

GIF๋กœ ๋ณด๋ฉด ์ž˜ ๋‚˜์˜ค๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•