+= 기존에 자기가 갖고 있던 값에서 추가된 값
-------------------
activity_main.xml
Design 탭에서 CheckBox와 Button 활용
<?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:layout_marginTop="32dp"
android:text="결과 텍스트"
android:textSize="36sp"
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="parent"
app:layout_constraintHorizontal_bias="0.498"
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:text="초록"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
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_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>
-------------------
-------------------
MainActivity.java
package com.example.checkbox;
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 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으로 텍스트 뷰에 출력
}
});
}
}
'안드로이드 앱 만들기' 카테고리의 다른 글
Frame Layout (뷰 끼리 겹치기) (0) | 2020.09.20 |
---|---|
Table Layout (0) | 2020.09.15 |
Radio Button (옵션 선택 버튼) (0) | 2020.09.12 |
뷰페이저 (ViewPager androidx ver) (0) | 2020.09.12 |
Firebase Recycler View (0) | 2020.09.12 |