ToggleButton: attributes (textOff, textOn), event methods (getTextOff(), getTextOn(), setChecked())

एक ToggleButton दो states वाला एक button है, on और off, हर एक के लिए textOn या textOff दिखाते हुए, और आप इसकी state isChecked() से पढ़ते हैं, इसे setChecked(true/false) से बदलते हैं, और इसके labels getTextOn()/getTextOff() से पढ़ते हैं।

8 min read · 9 cards · 2 checks

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


Theory

एक setting जो on या off याद रखे

FestConnect को एक 'Notifications' control चाहिए: reminders on करने के लिए एक बार tap कीजिए, फिर off करने के लिए फिर tap कीजिए, label बदलते हुए यह दिखाने के लिए यह किस state में है। एक सादा Button एक state याद नहीं रख सकता; यह बस fire होकर भूल जाता है।

ToggleButton exactly इसी के लिए बना है: यह एक on/off state रखता है, हर एक के लिए एक अलग label दिखाता है, और आपके code को वह state पढ़ने और set करने देता है। यह वह 'checked' idea भी introduce करता है जिसे CheckBox और RadioButton reuse करते हैं।

Theory

एक light switch, कोई doorbell नहीं

एक doorbell (एक सादा Button) आपके press करते ही कुछ करता है, फिर भूल जाता है। एक light switch (एक ToggleButton) आपके छोड़े जगह पर रहता है: ऊपर on है, नीचे off है, और आप इसकी position एक नज़र में देख सकते हैं। Switch presses के बीच एक state याद रखता है। 'मैं किस state में हूँ' की वह memory पूरा फ़र्क़ है।

Theory

Labels, state, और react करना

  • Labels: android:textOn (default 'ON') और android:textOff (default 'OFF') हर state के लिए text set करते हैं।
  • State पढ़िए: toggle.isChecked() true (on) या false (off) return करता है।
  • State set कीजिए: toggle.setChecked(true) इसे force on करता है।
  • Labels पढ़िए: getTextOn() / getTextOff()।
  • React कीजिए: toggle.setOnCheckedChangeListener((btn, isChecked) -> { ... }); यह flip होने पर चलता है।

(एक Switch sliding look वाला वही idea है।)

Practical

FestConnect notifications toggle

// XML: <ToggleButton android:id="@+id/notify"
//        android:textOn="Notifications On"
//        android:textOff="Notifications Off" />

ToggleButton notify = findViewById(R.id.notify);

notify.setOnCheckedChangeListener((btn, isChecked) -> {
    if (isChecked) {
        Toast.makeText(this, "Reminders on", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Reminders off", Toast.LENGTH_SHORT).show();
    }
});

// read or force the state anywhere:
boolean on = notify.isChecked();
notify.setChecked(true);   // turn it on from code

Think first

isChecked या setChecked?

आप (a) पता लगाना चाहते हैं notifications currently on हैं या नहीं, और (b) screen load होने पर उन्हें default रूप से on करना चाहते हैं। हर एक कौन सा method करता है, और हर एक क्या return या take करता है?

Show the answer

(a) Current state पता लगाने के लिए, isChecked() call कीजिए, जो एक boolean return करता है (true = on)। (b) Code से इसे on करने के लिए, setChecked(true) call कीजिए, जो एक boolean takes करता है और state set करता है (कोई return नहीं)। यह pattern checkable widgets के लिए universal है: isChecked() पढ़ता है, setChecked(...) लिखता है। आप अगली unit में CheckBox और RadioButton पर exactly यही pair इस्तेमाल करेंगे।

Quiz

एक ToggleButton पर android:textOn और android:textOff क्या control करते हैं, और इनके defaults क्या हैं?

  1. हर state में दिखाया गया label; defaults 'ON' और 'OFF' हैं
  2. हर state में button के colours
  3. Toggle होने पर बजने वाली sound
  4. क्या button enabled है या disabled
Show the answer

हर state में दिखाया गया label; defaults 'ON' और 'OFF' हैं

textOn और textOff on और off states के लिए दिखाया गया text label set करते हैं, defaults क्रमशः 'ON' और 'OFF' हैं (A)। वे colours (B) या sounds (C) set नहीं करते, और widget को enable/disable करने से इनका कोई लेना-देना नहीं (D)। इन्हें set करना (जैसे 'Notifications On'/'Notifications Off') toggle को self-explanatory बनाता है, जो एक settings screen पर अच्छा UI है।

Watch out

ToggleButton traps

1. एक सादे Button से state याद रखने की उम्मीद: यह नहीं रखता; on/off के लिए एक ToggleButton (या Switch/CheckBox) इस्तेमाल कीजिए।

2. Label को state से confuse करना: getTextOn() label return करता है; isChecked() boolean state return करता है। State जानने के लिए text test मत कीजिए।

3. ग़लत जगह state पढ़ना: इसके flip होते ही react करने के लिए, setOnCheckedChangeListener इस्तेमाल कीजिए, onCreate में एक one-time read नहीं।

Theory

Checked model scale up होता है

isChecked()/setChecked() और on/off idea सिर्फ़ ToggleButton के लिए नहीं हैं। Unit 5 CheckBox से खुलता है, जो identical checked model इस्तेमाल करता है एक student को कई events चुनने देने के लिए, फिर RadioButton, जो checkables को group करता है ताकि सिर्फ़ एक on हो सके। आप पहले से core method pair जानते हैं; अगले widgets बस बदलते हैं कितने एक साथ checked हो सकते हैं।

Summary

Key takeaways

  • एक ToggleButton एक two-state on/off value रखता है और हर state के लिए एक label दिखाता है।
  • android:textOn / android:textOff labels set करते हैं (defaults 'ON' / 'OFF')।
  • isChecked() state पढ़ता है (boolean); setChecked(true/false) इसे set करता है।
  • getTextOn()/getTextOff() labels पढ़ते हैं; setOnCheckedChangeListener इसके flip होने पर react करता है।
  • एक सादा Button state याद नहीं रख सकता; एक Switch एक slider-styled ToggleButton है।
  • Memory hook: एक light switch (अपनी जगह रहता है), कोई doorbell नहीं (fire होकर भूल जाता है)।

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 Android Widgets (UI)

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

ToggleButton: attributes (textOff, textOn), event methods (getTextOff(), getTextOn(), setChecked()) · Mobile Application Development - 1 (option B) · Gri-Learn