Theory
Exactly एक year
एक FestConnect student exactly एक year में है: First, Second, या Third, कभी दो नहीं। CheckBoxes उन्हें ग़लती से कई tick करने देंगे। आपको एक control चाहिए जहाँ एक option चुनना बाक़ी को clear कर दे।
यह है RadioButton, पर एक twist के साथ: अकेला एक RadioButton single choice enforce नहीं करता। Magic उन्हें एक RadioGroup में wrap करने से आता है। Group ही वह है जो buttons को mutually exclusive बनाता है, और यही सबसे ज़्यादा test किया जाने वाला fact है।
Theory
पुराने car radio buttons
Widget का नाम पुराने car radios से आया है: physical buttons की एक row जहाँ एक दबाना बाक़ी को pop out कर देता था, तो हमेशा सिर्फ़ एक station selected होता था। Row (RadioGroup) ही वह है जिसने उन्हें exclusive बनाया; एक अकेला button ऐसा नहीं कर सकता। Android RadioButtons वैसे ही काम करते हैं: उन्हें एक RadioGroup में डालिए और एक चुनना बाक़ी को release करता है।
Theory
Group इसे single-choice बनाता है
RadioButtons को एक `RadioGroup` में wrap कीजिए; यह 'सिर्फ़ एक selected' enforce करता है।- Selection ढूँढिए:
group.getCheckedRadioButtonId()selected button की id return करता है, या कोई नहीं तो -1। - फिर
findViewById(thatId)button लेता है; इसका text याisChecked()पढ़िए। - React कीजिए:
group.setOnCheckedChangeListener((grp, checkedId) -> { ... })।
RadioButtons Java में dynamically भी बनाए जा सकते हैं और group में जोड़े जा सकते हैं; custom वाले indicator restyle करते हैं।
Practical
FestConnect: अपना year चुनिए
// XML: <RadioGroup id=yearGroup> with 3 RadioButtons
// First (id=y1), Second (id=y2), Third (id=y3)
RadioGroup yearGroup = findViewById(R.id.yearGroup);
register.setOnClickListener(v -> {
int id = yearGroup.getCheckedRadioButtonId();
if (id == -1) {
Toast.makeText(this, "Please pick a year", Toast.LENGTH_SHORT).show();
return;
}
RadioButton chosen = findViewById(id);
Toast.makeText(this, "Year: " + chosen.getText(), Toast.LENGTH_SHORT).show();
});
// picking 'Second' automatically un-picks 'First' (the group enforces it)Think first
-1 का मतलब क्या है?
getCheckedRadioButtonId() एक situation में -1 return करता है। कब, और result पर findViewById call करने से पहले इसे check करना क्यों ज़रूरी है?
Show the answer
यह -1 return करता है जब group में कोई RadioButton selected नहीं है (user ने अभी तक एक year नहीं चुना)। यह matter करता है क्योंकि findViewById(-1) null return करता है, और null पर .getText() call करना crash करता है। तो आपको पहले if (id == -1) check करना ज़रूरी है और id पर भरोसा करने से पहले user को choose करने के लिए prompt करना ज़रूरी है। यह validate करने का RadioGroup version है कि एक required choice actually बनाई गई थी।
Quiz
तीन RadioButtons एक user को सिर्फ़ एक की बजाय तीनों एक साथ tick करने देते हैं। सबसे ज़्यादा संभावना कारण क्या है?
- वे एक single RadioGroup में wrapped नहीं हैं
- उनमें isChecked() method missing है
- RadioButtons को onCreate में setChecked(true) चाहिए
- RadioButtons कभी mutually exclusive नहीं हो सकते
Show the answer
वे एक single RadioGroup में wrapped नहीं हैं
Mutual exclusion RadioGroup से आता है; buttons को एक group में wrap किए बिना वे independent checkboxes जैसे behave करते हैं और सब select हो सकते हैं (A)। isChecked() एक read method है, कारण नहीं (B)। onCreate में setChecked call करना बस एक को pre-select करता, exclusion enforce नहीं करता (C)। RadioButtons group होने पर exactly वही mutually-exclusive widget हैं (D ग़लत है)। Fix: उन सबको एक RadioGroup के अंदर डालिए।
Watch out
RadioButton traps
1. बिना RadioGroup वाले RadioButtons: वे mutual exclusion खो देते हैं और checkboxes जैसे act करते हैं। हमेशा उन्हें group कीजिए।
2. -1 handle न करना: getCheckedRadioButtonId() -1 है जब कुछ नहीं चुना गया; findViewById(-1) null है। इस्तेमाल से पहले check कीजिए।
3. एक choice को दो groups में बाँटना: अलग-अलग RadioGroups में buttons एक-दूसरे से exclusive नहीं हैं। एक logical choice को एक group में रखिए।
Theory
अगला: dropdowns और dialogs
RadioButtons कुछ options के लिए अच्छे काम करते हैं, पर एक लंबी list (जैसे, 12 T-shirt sizes या 50 colleges) एक dropdown की तरह बेहतर है। अगला lesson Spinner (एक dropdown picker) को AlertDialog (एक modal yes/no box) के साथ pair करता है ताकि FestConnect एक size चुन सके और फिर submit करने से पहले registration confirm कर सके।
Summary
Key takeaways
- RadioButtons user को exactly एक option चुनने देते हैं, पर सिर्फ़ एक RadioGroup में wrapped होने पर (जो exclusion enforce करता है)।
- एक group में एक button चुनना automatically बाक़ी को clear कर देता है।
- group.getCheckedRadioButtonId() selected id return करता है, या कुछ नहीं चुना तो -1।
- findViewById(-1) null है, तो selected button पढ़ने से पहले -1 check कीजिए।
- एक-में-से-कई के लिए एक RadioGroup इस्तेमाल कीजिए; कई independent choices के लिए CheckBoxes इस्तेमाल कीजिए।
- Memory hook: पुराने car radio buttons, एक दबाना बाक़ी को pop out करता है।