CheckBox: event methods (isChecked(), setChecked()), default and custom Checkbox

एक CheckBox एक साथ कई options चुनने के लिए एक independently tickable on/off box है, और आप हर एक ticked है या नहीं यह isChecked() से पढ़ते हैं, इसे setChecked() से set करते हैं, और setOnCheckedChangeListener से changes पर react करते हैं।

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

जितने चाहें उतने चुनिए

एक FestConnect student Robotics और Dance और Coding attend कर सकता है, कोई भी combination। आपको एक control चाहिए जहाँ एक साथ कई options select हो सकें, independently।

यह है CheckBox: एक छोटा tickable box, हर option के लिए एक, हर एक अपने आप on या off। यह वही checked model share करता है जो आपने ToggleButton में देखा, पर यहाँ key idea है independence: एक ticking का बाक़ी पर कोई असर नहीं होता, जो exactly वही है जो 'जितने चाहें चुनिए' को चाहिए।

Theory

Tick boxes वाली एक shopping list

एक shopping list में हर item के बगल एक छोटा box होता है; आप हर item जो चाहें वह tick करते हैं, और 'milk' tick करने का 'bread' पर कोई असर नहीं। आप कोई नहीं, कुछ, या सब tick कर सकते हैं। CheckBoxes screen पर वही list हैं: हर box अपना ख़ुद decision है। एक RadioButton group (अगला lesson) से compare कीजिए, जो 'एक size चुनिए' जैसा ज़्यादा है, जहाँ Large चुनना Medium को un-pick कर देता है।

Theory

हर box independently पढ़िए

हर CheckBox अलग है, familiar checked methods के साथ:

  • `isChecked()`: क्या यह box ticked है? (boolean)
  • `setChecked(true/false)`: code से tick या untick कीजिए।
  • `setOnCheckedChangeListener(...)`: यह बदलने पर react कीजिए।

एक multi-choice answer collect करने के लिए, आप हर box का isChecked() अपने आप check करते हैं। एक default CheckBox standard tick दिखाता है; एक custom वाला android:button से indicator swap करता है, एक drawable/selector की तरफ़ point करते हुए।

Practical

FestConnect: अपने events चुनिए

// XML: three CheckBoxes with ids robotics, dance, coding
CheckBox robotics = findViewById(R.id.robotics);
CheckBox dance    = findViewById(R.id.dance);
CheckBox coding   = findViewById(R.id.coding);

register.setOnClickListener(v -> {
    StringBuilder chosen = new StringBuilder();
    if (robotics.isChecked()) chosen.append("Robotics ");
    if (dance.isChecked())    chosen.append("Dance ");
    if (coding.isChecked())   chosen.append("Coding ");

    Toast.makeText(this, "Events: " + chosen, Toast.LENGTH_LONG).show();
});

// each box is independent: ticking dance does NOT untick robotics

Think first

तीन अलग checks क्यों?

Code robotics, dance, और coding पर अलग-अलग isChecked() call करता है। 'selected event' के लिए एक check क्यों नहीं? यह आपको बताता है CheckBoxes एक radio group से कैसे अलग हैं?

Show the answer

क्योंकि एक student एक साथ कई events select कर सकता है, कोई अकेला 'selected event' नहीं है, हर box एक independent yes/no है, तो आपको हर एक से पूछना ज़रूरी है। वह independence exactly वह है जो CheckBoxes को एक RadioButton group से अलग करती है: एक radio group कई में से एक choice represent करता है (तो आप पूछते हैं 'कौन सा selected है?'), जबकि CheckBoxes कई independent choices represent करते हैं (तो आप हर एक से पूछते हैं 'क्या आप ticked हैं?')। Multi बनाम single पूरा distinction है।

Quiz

RadioButton group के बजाय आपको CheckBoxes कब इस्तेमाल करने चाहिए?

  1. जब user एक साथ एक से ज़्यादा option select कर सकता है
  2. जब user को exactly एक option चुनना ज़रूरी है
  3. जब आपको एक button चाहिए जो एक बार fire होकर भूल जाए
  4. जब आप एक छोटा auto-dismissing message दिखाना चाहते हैं
Show the answer

जब user एक साथ एक से ज़्यादा option select कर सकता है

CheckBoxes multi-select के लिए हैं: user कितने भी independent options tick कर सकता है (A)। Exactly एक चुनना एक RadioButton group का काम है (B)। एक fire-and-forget action एक सादा Button है (C), और एक छोटा auto-dismissing message एक Toast है (D)। याद रखिए: CheckBox = कई, RadioButton = एक। वह choice तय करती है आप कौन सा widget रखते हैं।

Watch out

CheckBox traps

1. CheckBoxes इस्तेमाल करना जब सिर्फ़ एक choice valid है: अगर options mutually exclusive हैं (एक अकेला size, एक अकेला year), CheckBoxes नहीं, एक RadioButton group इस्तेमाल कीजिए।

2. State के बजाय label test करना: boolean के लिए isChecked() इस्तेमाल कीजिए, text नहीं।

3. यह भूलना हर box independent है: कोई automatic 'बाक़ी को unselect करना' नहीं है; वह behaviour सिर्फ़ एक RadioGroup से आता है।

Theory

अगला: exactly एक चुनना

उल्टी ज़रूरत के लिए, एक अकेला year चुनना (First/Second/Third), CheckBoxes ग़लत हैं क्योंकि एक student सिर्फ़ एक year में है। अगला lesson RadioButton है, एक RadioGroup में grouped ताकि एक चुनना automatically बाक़ी को clear कर दे। यह isChecked() reuse करता है पर वह single-choice grouping जोड़ता है जिसका hint आपको यहाँ मिला।

Summary

Key takeaways

  • एक CheckBox एक independently tickable on/off box है, इस्तेमाल किया जाता है जब user कई options चुन सकता है।
  • isChecked() (boolean) से पढ़िए, setChecked() से set कीजिए, setOnCheckedChangeListener से react कीजिए।
  • हर box का isChecked() independently check करके एक multi-choice answer collect कीजिए।
  • एक CheckBox tick करना बाक़ी को affect नहीं करता (कोई automatic exclusion नहीं)।
  • कई choices के लिए CheckBoxes इस्तेमाल कीजिए; exactly एक के लिए एक RadioButton group इस्तेमाल कीजिए।
  • Memory hook: independent tick boxes वाली एक shopping list।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Other Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati