토글 버튼 Android의 켜기 / 끄기 텍스트 변경
방금 의 배경을 변경했으며ToggleButton
이제 그와 함께 표시되는 ON / OFF 텍스트를 변경하려고합니다. 이를 수행하는 가장 쉬운 방법은 무엇입니까?
다음을 사용하여 코드에서 텍스트를 설정할 수 있습니다.
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을 사용하여 텍스트를 설정하려면 다음을 사용하십시오.
android:textOff="The text for the button when it is not checked."
android:textOn="The text for the button when it is checked."
이 정보는 여기에서
링크 한 예에서, 그들은 android:textOn
and 를 사용하여 Day / Night로 변경 하고 있습니다.android:textOff
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" />
더 이상 toggleButton.setTextOff (textOff); 및 toggleButton.setTextOn (textOn) ;. 토글 된 각 상태의 텍스트는 관련 xml 특성을 포함하기 만하면 변경됩니다. 이것은 기본 ON / OFF 텍스트를 재정의합니다.
<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"/>
예, 켜짐 / 꺼짐 버튼을 변경할 수 있습니다.
android:textOn="Light ON"
android:textOff="Light OFF"
자세한 내용은 참조
http://androidcoding.in/2016/09/11/android-tutorial-toggle-button
다음 두 가지 옵션으로 수행 할 수 있습니다.
옵션 1 : xml 속성 설정
`android:textOff="TEXT OFF"
android:textOn="TEXT ON"`
옵션 2 : 프로그래밍 방식
onClick : methodNameHere 속성을 설정합니다 (내는 toggleState 임) 다음 코드를 작성합니다.
public void toggleState(View view) {
boolean toggle = ((ToogleButton)view).isChecked();
if (toggle){
((ToogleButton)view).setTextOn("TEXT ON");
} else {
((ToogleButton)view).setTextOff("TEXT OFF");
}
}
추신 : 그것은 나를 위해 작동합니다, 그것이 당신에게도 작동하기를 바랍니다
어떤 경우에는보기를 강제로 새로 고쳐야 작동합니다.
toggleButton.setTextOff(textOff);
toggleButton.requestLayout();
toggleButton.setTextOn(textOn);
toggleButton.requestLayout();
참고 URL : https://stackoverflow.com/questions/8673347/change-the-on-off-text-of-a-toggle-button-android
'Program Tip' 카테고리의 다른 글
JVM을 실행해야 할 때 Java 플랫폼에 어떻게 독립적입니까? (0) | 2020.10.15 |
---|---|
Enhanced for 문은 배열에 대해 어떻게 작동하며 배열에 대한 반복기를 가져 오는 방법은 무엇입니까? (0) | 2020.10.15 |
grunt를 설치하는 방법 및 스크립트를 작성하는 방법 (0) | 2020.10.15 |
이 부울 "(number & 1) == 0"은 무엇을 의미합니까? (0) | 2020.10.15 |
특정 열에 특정 문자열을 포함하는 Pandas 데이터 프레임에서 행을 삭제하는 방법은 무엇입니까? (0) | 2020.10.15 |