Android ToggleButton and androidbutton

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.