Android Basic Controls Togglebutton&switch use guide _android

Source: Internet
Author: User

ToggleButton (switch button) and switch (switches) to explain:

First, the core attribute explanation:

(1) ToggleButton

Texton: Text display when the button is selected

Textoff: Text display when the button is not selected

The status of the ToggleButton can only be checked and unchecked, and different display text needs to be set for different states.

The following case is the use of ToggleButton

Directory structure

Main.xml Layout file

<?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:layout_" height= "Fill_parent" >
  <imageview android:id= "@+id/imageview" android:layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:src= "@drawable/bulb_off" 
    android:layout_gravity= " Center_horizontal "/>
  <togglebutton android:id=" @+id/togglebutton "
    android:layout_width=" 140dip "
    android:layout_height= "wrap_content"
    android:texton= "turn on the Lights"
    android:textoff= "Turn Off the Lights"
    android: layout_gravity= "Center_horizontal"/>
</LinearLayout>

Togglebuttonactivity class

Package COM.LJQ.TB;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.CompoundButton;
Import Android.widget.ImageView;
Import Android.widget.ToggleButton;

Import Android.widget.CompoundButton.OnCheckedChangeListener;
  public class Togglebuttonactivity extends activity {private ImageView imageview=null;
  
  Private ToggleButton Togglebutton=null;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    
    Setcontentview (R.layout.main);
    imageview= (ImageView) Findviewbyid (R.id.imageview);
    togglebutton= (ToggleButton) Findviewbyid (R.id.togglebutton);  Togglebutton.setoncheckedchangelistener (New Oncheckedchangelistener () {public void oncheckedchanged (CompoundButton
        Buttonview, Boolean ischecked) {togglebutton.setchecked (ischecked); Imageview.setimageresource (ischecked?)
      R.drawable.bulb_on:r.drawable.bulb_off);
  }
      
    }); }
}

Operation Effect:

(2) Switch:

Showtext: Set Texton/off When the text is displayed

Android:showtext: Whether to display text when On/off is set, Boolean

Android:splittrack: Whether to set a gap, let the slider and the bottom of the picture separated, Boolean

Android:switchminwidth: Set the minimum width of the switch

Android:switchpadding: Sets the spacing of text within the slider

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 underline those

Android:track: Bottom of the picture

Android:thumb: Picture of the slider

You can try every single one of these properties.

When making a Bluetooth switch, use the switch, remember the usage, in fact, and button is almost the same.

In Layout:

<switch 
    android:id= "@+id/open" 
    android:layout_width= wrap_content "android:layout_height=" Wrap_ 
    Content " 
    android:textoff=" Bluetooth off 
    


In Java code

Open.setoncheckedchangelistener (New Oncheckedchangelistener () { 
 
      @Override public 
      void OnCheckedChanged ( Compoundbutton Buttonview, 
          boolean ischecked) { 
        //TODO auto-generated method Stub 
        if (ischecked) { 
          Mbluetoothadapter.enable ();//Open Bluetooth 
        } else { 
          mbluetoothadapter.disable ();//Close Bluetooth}}} 
    ); 

That's it, I see it.

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.