iOS를 사랑하는 AOS 개발자
[Android] CheckBox 간단 사용법 본문
반응형
SMALL
👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻
2022.01.03 수정
( 🤗 유튜브 오시면 나름 설명도 해줘요 🤗 )
반응형
회원가입을 할때나 뭔가 확인을 요구할때 자주 쓰이는 CheckBox 를 어떻게 하는지 간단하게 해봅시다!
언제나 그랫듯이 뷰부터 그립니다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1.2"
android:text="CHECK BOX"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"
/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#48F44336"
android:text="RED"
/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:text="ORANGE"
android:background="#61FF9800"
/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#5000ff00"
android:text="GREEN"
/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:text="BLUE"
android:background="#3403A9F4"
/>
</LinearLayout>
사용하고자 하는 CheckBox 개수 만큼 위젯을 작성해 줍니다!
😎 android:layout_weight 이라는게 나왔는데 이건 나중에 따로 자세하게 작성해보겠습니다!
자 이제 코드로 넘어갑니다
SMALL
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// 변수 선언
CheckBox[] checks; // checkbox 가 1개 이상이니 배열로 선언
Integer[] integers; // xml의 checkBox id 들을 담을 배열 선언 ( id 값의 return 형태는 integer )
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// checkbox 배열
checks = new CheckBox[4];
// xml의 checkBox id
integers = new Integer[]{ R.id.cb1, R.id.cb2, R.id.cb3, R.id.cb4 };
// for 문을 돌며 xml 의 checkBox id들과 연결
for (int i = 0; i < checks.length; i++) {
checks[i] = findViewById(integers[i]);
checks[i].setOnCheckedChangeListener(checkChangeListener);
}
}
// checkBox 를 체크할때마다 변경되는 리스너
CompoundButton.OnCheckedChangeListener checkChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ArrayList<String> arr = new ArrayList<>();
for (int i = 0; i < checks.length; i++){
if(checks[i].isChecked() == true){
arr.add(checks[i].getText().toString());
}
}
Toast.makeText(MainActivity.this, arr + " is checked.", Toast.LENGTH_SHORT).show();
}
};
}
CompoundButton.OnCheckedChangeListener 안에 사용되는
onCheckedChanged 메소드로 체크로 인한 이벤트를 확인 할 수 있어요!!!
이 메소드를 활용해서 원하는 로직을 구현 할 수 있겟죠?
전 단순하게 ArrayList 에 값을 담아 Toast만 사용했어요 ☺️
직접 해보세요!!!!!!!!!!!!!!!!! 🙋🏻♀️
이로써 코드는 끝 입니다
👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻
반응형
LIST
'Android ( JAVA ) > 개발' 카테고리의 다른 글
[Android] SlidingDrawer 간단 사용법 (0) | 2021.12.21 |
---|---|
[Android] RadioButton 간단 사용법(feat. strings.xml) (0) | 2021.12.21 |
[Android] FrameLayout 간단 사용법2 (0) | 2021.12.21 |
[Android] FrameLayout 간단 사용법 (0) | 2021.12.21 |
[Android] 안드로이드 앱 내에서 문자보내기 (0) | 2021.12.17 |
Comments