체크박스는 <CheckBox>를 사용한다.


On/Off 상태를 구할 땐 setOnCheckedChangeListener를 사용한다.


1
2
3
4
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
 
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"체크상태 : " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }
cs




+ Recent posts