2011-12-29 16 views

उत्तर

162

आप कोड से पाठ सेट करने के लिए निम्न का उपयोग कर सकते हैं:

toggleButton.setText(textOff); 
// Sets the text for when the button is first created. 

toggleButton.setTextOff(textOff); 
// Sets the text for when the button is not in the checked state. 

toggleButton.setTextOn(textOn); 
// Sets the text for when the button is in the checked state. 

एक्सएमएल का उपयोग करके पाठ सेट करने के लिए, का उपयोग करें:

android:textOff="The text for the button when it is not checked." 
android:textOn="The text for the button when it is checked." 

यह जानकारी here

+1

लेकिन यह e.i. पर काम नहीं करता सैमसंग और एचटीसी फोन – Serafins

+2

सेराफिन, यह सही नहीं है। यह सैमसंग और एचटीसी फोन पर काम करता है। – interrupt

+3

प्रोग्राम चालू रूप से चालू और बंद पाठ को अपडेट करते समय बटन नए टेक्स्ट के साथ खुद को फिर से नहीं चलाता है। आप setChecked (टॉगलबटन.इस चेक किए गए) को कॉल करके एक रेड्रो को मजबूर कर सकते हैं। हास्यास्पद लगता है लेकिन यह एक हैक redrawing मजबूर करने के लिए। [यह स्टैक ओवरफ्लो उत्तर] देखें (http://stackoverflow.com/a/3792554/2590478)। – MidasLefko

14
से है

उदाहरण के लिए आप लिंक करते हैं, वे इसे android:textOn और android:textOff

का उपयोग कर दिन/रात में बदल रहे हैं
+0

मैं वास्तव में एक अलग प्रश्न से जुड़ा हूं जिसे मैंने कभी नहीं देखा था। मेरे अपने प्रश्न में जवाब को इंगित करने के लिए धन्यवाद। – styler1972

6

XML के रूप में सेट करें:

<ToggleButton 
    android:id="@+id/flashlightButton" 
    style="@style/Button" 
    android:layout_above="@+id/buttonStrobeLight" 
    android:layout_marginBottom="20dp" 
    android:onClick="onToggleClicked" 
    android:text="ToggleButton" 
    android:textOn="Light ON" 
    android:textOff="Light OFF" /> 
1

ऐसा प्रतीत होता है तुम अब toggleButton.setTextOff (textOff) की जरूरत है; और टॉगलबटन.सेटटेक्स्टऑन (टेक्स्टऑन); प्रत्येक toggled राज्य के लिए पाठ केवल प्रासंगिक एक्सएमएल विशेषताओं सहित बदल जाएगा। यह डिफ़ॉल्ट चालू/बंद पाठ को ओवरराइड करेगा।

<ToggleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/toggleText" 
    android:textOff="ADD TEXT" 
    android:textOn="CLOSE TEXT" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="10dp" 
    android:visibility="gone"/> 
संबंधित मुद्दे