android-Custom Switch Button implementation

Source: Internet
Author: User

In the application we often see some of the selection switch status of the configuration file, when the project is using the Android switch control, but feel good ugly look ...

Personally think it is better to customize, first of all:

Implementation process:

1. Prepare two images for the switch in different states into the drawable.

Add code to the 2.xml file:

    <togglebutton        android:id= "@+id/switch1"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:layout_gravity= "center"        android:background= "@android: Color/transparent"        android:button= "@drawable/btn_backgrounds"        android:checked= "false"        android:textoff= ""        Android:texton= ""/>
android:button= "@drawable/btn_backgrounds" is your own definition of Android selector file:
<selector  xmlns:android= "http://schemas.android.com/apk/res/android" >    <item android:state_ Checked= "true" android:drawable= "@drawable/toggle_btn_on"/>    <item android:drawable= "@drawable/toggle_ Btn_off "/>

3, then declare the ToggleButton control in the Java file and instantiate it, adding the Click event:

    /*** Self out created inner class, listen button click event * *@authorCYF **/    classMyoncheckedchangelistenerImplementsOncheckedchangelistener {@Override Public voidOnCheckedChanged (Compoundbutton arg0,Booleanarg1) {            if(arg1) {  // open               } Else {                //off    }        }    }

4. Add Monitoring:

New Myoncheckedchangelistener ());

Note Memo, this does not have a switch sliding effect ~ ~ ~

android-Custom Switch Button implementation

Related Article

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.