ToggleButton Control for android development and common controls for android Development
ToggleButton can be considered as a switch. Each click is switched between "on" and "off.
Common ToggleButton methods:
1. setTextOff ()
The text displayed when it is not selected.
2. setTextOn ()
The text displayed when selected.
3. setCheaked ()
Set the control status.
4. setBackgroundDrawable ()
Set the background of the control.
Controls are easy to use.
Demo instance:
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 = "" sets the initial background. Otherwise, the default background is displayed. android: background = "@ 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:
**************************************** *
I have found several button pictures. It's hard to see them. Let's just make it happen.
**************************************** ***
Not pressed:
Press: