2011-12-29 11 views

risposta

162

È possibile utilizzare le seguenti operazioni per impostare il testo dal codice:

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. 

per impostare il testo utilizzando XML, utilizzare il seguente:

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

Questa informazione è da here

+1

tuttavia non funziona su E.I. telefoni Samsung e htc – Serafins

+2

Serafini, non è giusto. Funziona su telefoni Samsung e HTC. – interrupt

+3

Quando si aggiorna il testo on e off in modo programmatico, il pulsante non si ridisegna con il nuovo testo. È possibile forzare un ridisegno chiamando setChecked (toggleButton.isChecked). Sembra ridicolo ma è un trucco per forzare il ridisegno. Vedi [questa risposta StackOverflow] (http://stackoverflow.com/a/3792554/2590478). – MidasLefko

14

Nell'esempio a cui ci si collega, lo stanno cambiando in Giorno/Notte usando android:textOn e android:textOff

+0

In realtà mi sono collegato a una domanda diversa che non avevo mai visto prima. Grazie per aver indicato la risposta nella mia stessa domanda. – styler1972

6

Impostare l'XML come:

<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

Sembra non è più necessario toggleButton.setTextOff (textOff); e toggleButton.setTextOn (textOn) ;. Il testo per ogni stato attivato cambierà semplicemente includendo le caratteristiche xml rilevanti. Questo sostituirà il testo ON/OFF predefinito.

<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"/> 
Problemi correlati