Android 0 Basics Section 21st: ToggleButton and switch usage Daquan

Source: Internet
Author: User

In the previous period, the checkbox and RadioButton have been studied, so the other two sub-controls ToggleButton and switch of the button in this issue are equally important in development.

First, ToggleButton

The ToggleButton (switch button) is a relatively simple component of the Android system, a button with checked and unchecked double states, and requires different display text for different states. A Oncheckedchange event is triggered when the user switches between the two states.

The XML attributes and related methods supported by ToggleButton are shown in the following table.

Next, learn the usage of ToggleButton using a simple example program.

Also use the Widgetsample project, create a togglebutton_layout.xml file in the app/main/res/layout/directory, and populate it with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Do you like to learn Android development?" "android:textsize= "22SP"/>    <ToggleButtonAndroid:id= "@+id/like_tb"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:texton= "like"Android:textoff= "Don't like" /></LinearLayout>

Then modify the App/src/java/mainactivity.java file to load the layout file as the new Togglebutton_layout.xml file. In order to listen for the toggle event of a button, add an event listener to it in the Java code, with the following code:

 Packagecom.jinyu.cqkxzsxy.android.widgetsample;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.Toast;ImportAndroid.widget.ToggleButton; Public classMainactivityextendsappcompatactivity {PrivateToggleButton MLIKETB =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.togglebutton_layout); //Get interface ComponentsMLIKETB =(ToggleButton) Findviewbyid (R.ID.LIKE_TB); //Setting the Oncheckedchangelistener Listener for the switch buttonMliketb.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Compoundbutton,Booleanb) {//message Prompt                if(compoundbutton.ischecked ()) {Toast.maketext (mainactivity). This,                            "Like Android Development", Toast.length_short). Show (); } Else{toast.maketext (mainactivity). This,                            "Don't like Android development", Toast.length_short). Show ();    }            }        }); }}

Running the program, you can see the interface effect shown.

Second, Switch

Switch is a switch control that can be toggled between two state transitions. The user can drag to select, or click to toggle switch status just as you would select a check box. When the state changes, a Oncheckedchange event is triggered.

The XML attributes and related methods supported by switch are shown in the following table.

Next, use a simple example program to learn about switch usage.

Also use the Widgetsample project, create a switch_layout.xml file in the app/main/res/layout/directory, and populate it with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Turn Bluetooth on/off"android:textsize= "22SP"/>    <SwitchAndroid:id= "@+id/bluetooth_switch"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

Then modify the App/src/java/mainactivity.java file to load the layout file as the new Switch_layout.xml file. In order to listen for the Click event of the Switch button, add the switch event listener to the Java code as follows:

 Packagecom.jinyu.cqkxzsxy.android.widgetsample;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.Switch;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateSwitch Mbluetoothswitch =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.switch_layout); //Get interface ComponentsMbluetoothswitch =(Switch) Findviewbyid (R.id.bluetooth_switch); //to bind the Oncheckedchangelistener listener to the switch buttonMbluetoothswitch.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Compoundbutton,Booleanb) {if(compoundbutton.ischecked ()) {Toast.maketext (mainactivity). This, "Turn on Bluetooth", Toast.length_short). Show (); } Else{toast.maketext (mainactivity). This, "Turn off Bluetooth", Toast.length_short). Show ();    }            }        }); }}

Run the program, toggle the status of the switch, you can see the interface effect shown.

Here, the two button subcomponents ToggleButton and switch have learned to complete, have you mastered it?

-----------------------------------------

Come here today, the next issue begins the UI component learning. If you have questions welcome message together to explore, also welcome to join the Android 0 Basic introductory Technology discussion group, grow together!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: TextView Properties and Methods Daquan

Android 0 Basics Section 18th: EditText properties and how to use them

Android 0 Basics section 19th: Button usage explained

Android 0 Basics Section 20th: checkbox and RadioButton Usage Daquan

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if reproduced please note source, hereby declare!

Android 0 Basics Section 21st: ToggleButton and switch usage Daquan

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.