findViewById<CheckBox>(R.id.checkbox_meat).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Meat is checked: $isChecked")}findViewById<CheckBox>(R.id.checkbox_cheese).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Cheese is checked: $isChecked")}
Java
findViewById<CheckBox>(R.id.checkbox_meat).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Meat is checked: $isChecked");}findViewById<CheckBox>(R.id.checkbox_cheese).setOnCheckedChangeListener{buttonView,isChecked->
Log.d("CHECKBOXES","Cheese is checked: $isChecked");}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Add checkboxes to your app\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Checkbox →](/develop/ui/compose/components/checkbox) \n| **Note:** For a better user experience, see the [Material Design Checkbox](https://m3.material.io/components/checkbox/overview) documentation.\n\nCheckboxes let the user select one or more options from a set. Typically, you present checkbox\noptions in a vertical list.\n**Figure 1.** An example of checkboxes from [Material Design Checkbox](https://m3.material.io/components/checkbox/guidelines).\n\nTo create each checkbox option, create a\n[CheckBox](/reference/android/widget/CheckBox) in your layout. Because\na set of checkbox options lets the user select multiple items, each checkbox is managed separately,\nand you must register a click listener for each one.\n\nRespond to click events\n-----------------------\n\nBegin by creating a layout with `CheckBox` objects in a list: \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:orientation=\"vertical\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n \u003cCheckBox android:id=\"@+id/checkbox_meat\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Meat\" /\u003e\n \u003cCheckBox android:id=\"@+id/checkbox_cheese\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"Cheese\"/\u003e\n\u003c/LinearLayout\u003e\n```\n\nOnce your layout is ready, head to your `Activity` or `Fragment`, find your\n`CheckBox` views, and set a change listener, as in the following example: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_meat)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Meat is checked: $isChecked\")\n }\n\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_cheese)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Cheese is checked: $isChecked\")\n }\n```\n\n### Java\n\n```java\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_meat)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Meat is checked: $isChecked\");\n }\n\nfindViewById\u003cCheckBox\u003e(R.id.checkbox_cheese)\n .setOnCheckedChangeListener { buttonView, isChecked -\u003e\n Log.d(\"CHECKBOXES\", \"Cheese is checked: $isChecked\");\n }\n```\n\nThe previous code prints a message in Logcat every time the checkboxes change status.\n| **Tip:** If you need to change the checkbox state yourself, use the [setChecked(boolean)](/reference/android/widget/CompoundButton#setChecked(boolean)) or [toggle()](/reference/android/widget/CompoundButton#toggle()) method."]]