Pola wyboru pozwalają użytkownikowi na wybranie jednej lub kilku opcji z wielu dostępnych. Zwykle opcje w postaci pól wyboru przedstawia się na liście pionowej.
Aby utworzyć opcję pola wyboru, utwórz w układzie elementCheckBox. Zestaw opcji pola wyboru umożliwia użytkownikowi wybranie wielu elementów, dlatego każdym polem wyboru należy zarządzać osobno i dla każdego z nich należy zarejestrować odbiornik kliknięcia.
Odpowiadanie na zdarzenia kliknięcia
Zacznij od utworzenia układu z CheckBox obiektami na liście:
Gdy układ będzie gotowy, otwórz plik Activity lub Fragment, znajdź widoki CheckBox i ustaw odbiornik zmian, jak w tym przykładzie:
Kotlin
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");}
Poprzedni kod drukuje wiadomość w Logcat za każdym razem, gdy pola wyboru zmieniają stan.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-27 UTC.
[null,null,["Ostatnia aktualizacja: 2025-07-27 UTC."],[],[],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."]]