Android Development ToggleButton Controls

Source: Internet
Author: User

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

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.