Android ToggleButton and androidbutton
ToggleButton is a simple component in the Android system. It is a button with two States selected and not selected, and different display text needs to be set for different states.
Common XML attributes of ToggleButton
Attribute name |
Description |
Android: disabledAlpha |
Set the transparency when the button is disabled. |
Android: textOff |
Text of the button that is not selected |
Android: textOn |
Text of the selected button
|
Main. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: background = "# FFF5F5F5" android: layout_height = "fill_parent"> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <TextView android: textSize = "14.0sp" android: id = "@ + id/tvSound" android: textColor = "@ android: color/black" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "enabled"/> <ToggleButton android: id = "@ + id/tglSound" android: background = "@ drawable/selector_butn_toggle" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "true" android: textOn = "" android: textOff = "" android: text = ""/> </LinearLayout>
MainActivity. java:
Package com.apk bus. toggle; import android. app. activity; import android. OS. bundle; import android. view. window; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. textView; import android. widget. toggleButton; public class MainActivity extends Activity implements OnCheckedChangeListener {private ToggleButton mToggleButton; private TextView tvSound; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // hide the title bar setContentView (R. layout. main); initView (); // initialization control method} private void initView () {mToggleButton = (ToggleButton) findViewById (R. id. tglSound); // obtain the mToggleButton of the control. setOnCheckedChangeListener (this); // Add the listener event tvSound = (TextView) findViewById (R. id. tvSound) ;}@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {tvSound. setText ("enabled");} else {tvSound. setText ("disabled ");}}}
:
Demo: http://download.csdn.net/detail/u010963246/8941969
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.