My android learning experience 13 and android learning experience 13
Use of the ToggleButton Control
The ToggleButton control can be identified by its name as a "Switch" control, that is, there are two buttons in different States.
There are three main special attributes:
Android: textOn = "on" ---- text displayed when the status is true
Android: textOff = "off" ---- text displayed when the status is false
Android: checked = "true" ---- identify status
The following is a simple example to illustrate how ToggleButton is used.
Two controls are required. One is ToggleButton and the other is TextView. The function is to display different texts in different statuses of ToggleButton (more complex operations can be implemented, please write as needed)
The layout file code is:
<ToggleButton
Android: id = "@ + id/toggleButton1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: textOn = "open"
Android: textOff = "off"
Android: checked = "true"/>
<! -- Android: checked = "true" attribute mainly displays whether the object is selected or not. If no data is written, it is deselected by default. -->
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/toggleButton1"
Android: layout_alignParentRight = "true"
Android: layout_below = "@ + id/toggleButton1"
Android: layout_marginTop = "31dp"
/>
The content in the source code file is as follows:
Private ToggleButton tgB;
Private TextView TV;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TgB = (ToggleButton) findViewById (R. id. toggleButton1 );
TV = (TextView) findViewById (R. id. textView1 );
TgB. setOnCheckedChangeListener (new OnCheckedChangeListener (){
Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// Display different texts based on different statuses of the ToggleButton Control
If (isChecked ){
TV. setText ("On Now ");
TV. setTextSize (30f );
} Else {
TV. setText ("the status is off now ");
TV. setTextSize (30f );
}
}
});
}
Interface:
Please advise if something is wrong. Thank you.