Android learning notes-ToggleButton and Switch, togglebuttonswitch

Source: Internet
Author: User

Android learning notes-ToggleButton and Switch, togglebuttonswitch

Reference: http://www.runoob.com/w3cnote/android-tutorial-togglebutton-switch.html

 

The basic Android UI controls described in this section are: ToggleButton and Switch. Both are actually Switch components, but the latter must be used after Android 4.0. Therefore, AndroidManifest. the minsdk in the xml file must be greater than or equal to 14. Otherwise, an error is returned ~, Let's take a look at what the two controls look like first. After Android 5.0, these two controls look much better than before. Let's take a look at what we looked like before Android 5.0:

ToggleButton and Switch before 5.0: 

 

Version 5.0:

Well, in stark contrast... let's take a look at the use of the two controls. In fact, the two are almost the same.

Paste the official API first: Switch; ToggleButton

1. Explanation of core attributes: 1) ToggleButton (switch button)

The following attributes can be set:

 
 
  • Android: disabledAlpha: Sets the transparency of the button when it is disabled.
  • Android: textOff:Text displayed when the button is not selected
  • Android: textOn:The text displayed when the button is selected. In addition to this, you can also write a selector and set the Background attribute ~
2) Switch)

The following attributes can be set:

 
 
  • Android: showText:Whether to display text when setting on/off, boolean
  • Android: splitTrack:Whether to set a gap to separate the slider from the bottom image, boolean
  • Android: switchMinWidth:Set the minimum width of the switch.
  • Android: switchPadding:Set the text interval in the slider
  • Android: switchTextAppearance:Set the text appearance of the switch. Nothing is found at the moment...
  • Android: textOff:Text displayed when the button is not selected
  • Android: textOn:Text displayed when the button is selected
  • Android: textStyle:Text style, bold, italic-style dashes
  • Android: track:Bottom Image
  • Android: thumb:Slider Image
  • Android: typeface:Set the font. The three types of fonts are supported by default: sans, serif, and monospace. You can also use other font files (*. Ttf), First save the font file in the assets/fonts/directory, but you need to set it in Java code:Typeface typeFace = Typeface. createFromAsset (getAssets (), "fonts/HandmadeTypewriter. ttf"); textView. setTypeface (typeFace );
2. Example:

Because it is relatively simple, we write them together. In addition, we set the slider and the image at the bottom for the Switch to achieve a slider effect similar to IOS 7, however, the disadvantage is that you cannot set the size of the slider and the bottom in XML, that is, the size of the material and the size of the Switch. We can obtain the Drawable object in Java, then modify the size. A simple example is as follows:

Run:

Implementation Code:First, two drawable files:Thumb_selctor.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/switch_btn_pressed"/>    <item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/></selector>

Track_selctor.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true" android:drawable="@drawable/switch_btn_bg_green"/>    <item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/></selector>

Layout file: 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 = ". mainActivity "> <ToggleButton android: id =" @ + id/tbtn_open "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: checked =" true "android: textOff = "Disable sound" android: textOn = "enable sound"/> <Switch android: id = "@ + id/swh_status" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textOff = "" android: textOn = "" android: thumb = "@ drawable/thumb_selctor" android: track = "@ drawable/track_selctor"/> </LinearLayout>

MainActivity. java:

Public class MainActivity extends AppCompatActivity implements CompoundButton. onCheckedChangeListener {private ToggleButton tbtn_open; private Switch swh_status; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); tbtn_open = (ToggleButton) findViewById (R. id. tbtn_open); swh_status = (Switch) findViewById (R. id. swh_status); invoke (this); swh_status.setOnCheckedChangeListener (this) ;}@ Override public void onCheckedChanged (CompoundButton compoundButton, boolean B) {switch (compoundButton. getId () {case R. id. tbtn_open: if (compoundButton. isChecked () Toast. makeText (this, "enable sound", Toast. LENGTH_SHORT ). show (); else Toast. makeText (this, "enable sound", Toast. LENGTH_SHORT ). show (); break; case R. id. swh_status: if (compoundButton. isChecked () Toast. makeText (this, "Switch: ON", Toast. LENGTH_SHORT ). show (); else Toast. makeText (this, "Switch: OFF", Toast. LENGTH_SHORT ). show (); break ;}}}

 

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.