ToggleButton can be thought of as a switch that switches between on and off once per click.
Several common methods of ToggleButton:
1.SETTEXTOFF ()
The text that is displayed when it is not selected.
2.setTextOn ()
The text that is displayed when it is selected.
3.setCheaked ()
Sets the state of the control.
4.setBackgroundDrawable ()
Sets the background of the control.
Control is simple to use.
Demo Example:
Mainactivity.java
public class Mainactivity extends Activity {ToggleButton ToggleButton; @Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ToggleButton = ( ToggleButton) Findviewbyid (r.id.tbut); Togglebutton.setoncheckedchangelistener (new Oncheckedchangelistener () {@ overridepublic void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {if (arg1) {Togglebutton.setbackgroundresource (R.drawable.open);} Else{togglebutton.setbackgroundresource (R.drawable.close);}});}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <!-- The display text is empty android:textoff= "" android:texton= "" to set the initial background, otherwise the default background android:background= is displayed "@ Drawable/close " - <togglebutton android:id=" @+id/tbut " android:layout_width=" 40DP " android:layout_height= "40DP" android:layout_gravity= "center_horizontal" android:background= "@ Drawable/close " android:textoff=" " android:texton=" "/></linearlayout>
Figure:
*****************************************
Button picture looked for several, not good-looking, how to see how ugly, this will make it.
*******************************************
Not pressed:
Press the following:
Android Development ToggleButton Controls