촬리의늘솔길

앱만들기.36(Check Box) 본문

✍~2022/app(android studio)

앱만들기.36(Check Box)

리촬리 2021. 8. 16. 00:39

checkbox와 라디오박스의 차이점을 하면서 짚어보자!

 

라디오버튼은 라디오 그룹이라는 부모형태의 그루핑 되어서 나타나는데,

체크박스는 단독으로 가능

 

MainActivity.java

package com.example.checkboxexample;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private CheckBox chk_red,chk_blue,chk_green;
    private TextView tv_result;
    private Button btn_result;

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

        chk_red = findViewById(R.id.chk_red);
        chk_blue = findViewById(R.id.chk_blue);
        chk_green = findViewById(R.id.chk_green);
        tv_result= findViewById(R.id.tv_result);
        btn_result = findViewById(R.id.btn_result);


        btn_result.setOnClickListener(new View.OnClickListener() {//결과버튼을 클릭했을때 액션
            @Override
            public void onClick(View v) {
                String str_result = "";//공백의 값을 하나 넣어줌, 클릭이 될때마다String 초기화
                if(chk_red.isChecked()){ //빨강 체크박스에 체크가 되어있다면 실행
                    str_result += chk_red.getText().toString();

                } if(chk_blue.isChecked()){ // 파랑체크박스에 체크가 되어있다면
                    str_result += chk_blue.getText().toString();

                } if(chk_green.isChecked()){ //초록체크박스에 체크가 되어있다면
                    str_result += chk_green.getText().toString();

                }
                tv_result.setText(str_result); //체크박스에 체크되어있던 값을 스트링으로 출력

            }});




    }
}

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

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="결과 텍스트!"
        android:textSize="34sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_result" />

    <CheckBox
        android:id="@+id/chk_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="빨강"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/chk_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="파랑"
        app:layout_constraintEnd_toEndOf="@+id/chk_red"
        app:layout_constraintHorizontal_bias="0.524"
        app:layout_constraintStart_toStartOf="@+id/chk_red"
        app:layout_constraintTop_toBottomOf="@+id/chk_red" />

    <CheckBox
        android:id="@+id/chk_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="초록"
        app:layout_constraintEnd_toEndOf="@+id/chk_blue"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/chk_blue"
        app:layout_constraintTop_toBottomOf="@+id/chk_blue" />

    <Button
        android:id="@+id/btn_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="선택 완료"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_green" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

728x90