2swan

Check box (옵션 선택 버튼) 본문

Programming/Android Function

Check box (옵션 선택 버튼)

2swan 2023. 8. 5. 14:30

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=".MainActivity4">

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="182dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="171dp"
        android:text="결과 텍스트"
        android:textSize="34sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="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_marginStart="157dp"
        android:layout_marginTop="101dp"
        android:layout_marginEnd="195dp"
        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:layout_marginStart="157dp"
        android:layout_marginEnd="195dp"
        android:text="파랑"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_red" />

    <CheckBox
        android:id="@+id/chk_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="157dp"
        android:layout_marginEnd="195dp"
        android:text="초록"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        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_marginStart="151dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="169dp"
        android:text="버튼"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_green" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

main.activity

public class MainActivity4 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_main4);

        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 view) {        //결과 버튼을 클릭했을 때 액션
                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);  //체크박스에 체크되어있던 값을 String으로 텍스트뷰에 출력
            }
        });

    }
}

 


결과 값

'Programming > Android Function' 카테고리의 다른 글

SharedPreferences (임시저장)  (0) 2023.08.05
Radio Button (옵션 선택 버튼)  (0) 2023.08.05
Spinner 드롭다운  (0) 2023.08.05
액티비티 생명주기(Life Cycle)  (0) 2023.08.05
WebView  (0) 2023.08.04