About Android development implementation of the two-state button

Source: Internet
Author: User

Dual status button (Toggle)

XML file code

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6<imageview android:layout_width="wrap_content"7android:layout_height="wrap_content"8Android:src="@drawable/bulb_off"9Android:id="@+id/imageview"Tenandroid:layout_gravity="Center_horizontal" One/> A<togglebutton android:layout_width="140dip" -android:layout_height="wrap_content" -android:texton="Open" theandroid:textoff="Close" -Android:id="@+id/togglebutton" -android:layout_gravity="Center_horizontal" -/> +  -</LinearLayout>
XML File


Java files

1 Package Lianxi;2 3 import Java.security.PublicKey;4 5 import Com.example.jichu_lianxi. R;6 7 import android.app.Activity;8 import Android.os.Bundle;9 import Android.widget.CompoundButton;Ten import Android.widget.CompoundButton.OnCheckedChangeListener; One import Android.widget.ImageView; A import Android.widget.ToggleButton; - /* - * Double button the  */ -  Public classTogglebutton_lianxi extends activity{ -     PrivateImageView ImageView =NULL;//Picture Label -     PrivateToggleButton ToggleButton =NULL;//Two-state button +      - @Override +     protected voidonCreate (Bundle savedinstancestate) { A         //TODO auto-generated Method Stub at super.oncreate (savedinstancestate); - Setcontentview (R.layout.togglebutton_lianxi); -      -ImageView =(ImageView) Findviewbyid (R.id.imageview); -ToggleButton =(ToggleButton) Findviewbyid (R.id.togglebutton); -      in     //Set Button Response -Togglebutton.setoncheckedchangelistener (NewOncheckedchangelistener () { to          + @Override -          Public voidoncheckedchanged (Compoundbutton Buttonview, Boolean isChecked) { the             //TODO auto-generated Method Stub * togglebutton.setchecked (isChecked); $             //set up a picturePanax NotoginsengImageview.setimageresource (isChecked?R.drawable.bulb_on:r.drawable.bulb_off); -         } the     }); +      A     } the          +}
Togglebutton_lianxi.java


About Android development implementation of the two-state button

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.