ToggleButton Switch Status button control, togglebutton set status

Source: Internet
Author: User

ToggleButton Switch Status button control, togglebutton set status
ToggleButton Switch Status button control

I. Introduction

1,

2. ToggleButton Class Structure

The parent class is CompoundButton.

 

Ii. How to Use the ToggleButton Control

1. Create a ToggleButton control and an object

Private ToggleButton toggleButton1;

ToggleButton1 = (ToggleButton) findViewById (R. id. toggleButton1 );


2. Set the setOnCheckedChangeListener Method

ToggleButton1.setOnCheckedChangeListener (new OnCheckedChangeListener (){})


3. perform operations based on whether the checked method is used.

If (isChecked) {// open
LinearLayout1.setOrientation (LinearLayout. VERTICAL );
}
Else {// off
LinearLayout1.setOrientation (LinearLayout. HORIZONTAL );
}

 

Iii. code example

1 ,:

On

Off status

 

2. Code:

Fry. Activity01

1 package fry; 2 3 import com. example. toggleButtonDemo1.R; 4 5 import android. app. activity; 6 import android. OS. bundle; 7 import android. widget. compoundButton; 8 import android. widget. compoundButton. onCheckedChangeListener; 9 import android. widget. linearLayout; 10 import android. widget. toggleButton; 11 12 public class Activity01 extends Activity {13 private LinearLayout linearLayout1; 14 private ToggleButton toggleButton1; 15 16 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 // TODO Auto-generated method stub21 super. onCreate (savedInstanceState); 22 setContentView (R. layout. activity01); 23 24 linearLayout1 = (LinearLayout) findViewById (R. id. linearLayout1); 25 toggleButton1 = (ToggleButton) findViewById (R. id. toggleButton1 ); 26/* 27 * use ToggleButton control on/off STATUS 28*1. Create ToggleButton control and object 29*2. Set setOnCheckedChangeListener to 30*3. perform operations 31 based on whether the checked method is used * 32 */33 toggleButton1.setOnCheckedChangeListener (new OnCheckedChangeListener () {34 35 @ Override36 public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {37 // TODO Auto-generated method stub38 if (isChecked) {// open 39 rows (LinearLayout. VERTICAL); 40} 41 else {// off 42 linearLayout1.setOrientation (LinearLayout. HORIZONTAL); 43} 44} 45}); 46 47} 48}

/ToggleButtonDemo1/res/layout/activity01.xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <ToggleButton 8 android: id = "@ + id/toggleButton1" 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content" 11 android: checked = "true" 12 android: textOn = "horizontal arrangement" 13 android: textOff = "Vertical arrangement" 14/> 15 <LinearLayout 16 android: id = "@ + id/linearLayout1" 17 android: layout_width = "wrap_content" 18 android: layout_height = "wrap_content" 19 android: orientation = "vertical" 20> 21 <Button 22 android: layout_width = "wrap_content" 23 android: layout_height = "wrap_content" 24 android: text = "button" 25/> 26 <Button 27 android: layout_width = "wrap_content" 28 android: layout_height = "wrap_content" 29 android: text = "button" 30/> 31 <Button 32 android: layout_width = "wrap_content" 33 android: layout_height = "wrap_content" 34 android: text = "button" 35/> 36 </LinearLayout> 37 38 </LinearLayout>

 

4. Obtain

1,

Android: checked = "true"

Set ToggleButton status

2,

Android: textOn = "horizontal arrangement"

Set ToggleButton to Open Text

3,

ToggleButton1.setOnCheckedChangeListener (new OnCheckedChangeListener (){})

SetOnCheckedChangeListener Method for ToggleButton

4,

If (isChecked)

ToggleButton status check switch

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.