vous pouvez définir dans le bouton à bascule on/off en XML comme ci-dessous
// code XML pour le bouton à bascule
<ToggleButton
android:id="@+id/toggBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:textOn="On" //when your button on by clicking "On" text will be displayed
android:textOff="Off" /> //When you off the toggle button "Off" text will be displayed
dans le code java.
ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggBtn);
toggleButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(toggleButton.getText().toString().equals("on")){
////do when toggle button is on
}else if(toggleButton.getText().toString().equals("off")){
// do when toggle button is off
}
}
});
Pas plus de conseils, s'il vous plaît, n'importe qui?? – user788511
La meilleure façon de tenter cela est: ToggleButton btn = new ToggleButton (this); btn.setTextOn (valeur de chaîne); btn.setTextOff (valeur de chaîne); – user788511