2011-12-29 13 views

cevap

162

Sen kodundan metni ayarlamak için aşağıdaki kullanabilirsiniz:

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

Bu bilgiler here

+1

samsung ve htc telefonları – Serafins

+2

Serafins, bu doğru değil. Samsung ve HTC telefonlarında çalışır. – interrupt

+3

Metni açık ve kapalı olarak programlı olarak güncellerken, düğme yeni metinle kendini yeniden çizmez. SetChecked (toggleButton.isChecked) çağırarak yeniden çizmeyi zorlayabilirsiniz. Saçma geliyor ama yeniden çizilmeye zorlanıyor. Bkz. [Bu stackoverflow yanıtı] (http://stackoverflow.com/a/3792554/2590478). – MidasLefko

14
şöyledir:

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. 

xml kullanarak metin ayarlamak için aşağıdaki kullanın

Bağlantı kurduğunuz örnekte, android:textOn ve android:textOffandroid:textOff

kullanarak Gündüz/Gece olarak değiştirilir
+0

Aslında daha önce hiç görmediğim farklı bir soruyla bağlantı kurdum. Cevabımı kendi sorum- lumda gördüğünüz için teşekkürler. – styler1972

6

olarak XML ayarlayın: artık toggleButton.setTextOff (textOff) ihtiyaç belirir

<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

; ve toggleButton.setTextOn (textOn); Her bir değiştirilen durum için metin, yalnızca ilgili xml özelliklerini içerecek şekilde değişecektir. Bu, varsayılan ON/OFF metnini geçersiz kılar.

<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"/> 
İlgili konular