Android Basics Two ToggleButton

Source: Internet
Author: User

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= "com.vincentlin.togglebutton.MainActivity"  > <!--  texton:true  textoff:false -->    <togglebutton         android:id= "@+id/togglebutton1"          Android:checked= "false"         android:texton= "@string/on"          android:textoff= "@string/off"          android:layout_width= "Match_parent"         android:layout_height = "Wrap_content"         />    <imageview         android:id= "@+id/imageview1"         android:layout_ Width= "Match_parent"         android:layout_height= "Match_parent"          android:background= "@drawable/off"  /></LinearLayout>
Strings.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >togglebutton</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_settings" >Settin Gs</string> <string name= "on" > Open </string> <string name= "Off" > Off </string></resources >
Mainactivity.java
Package com.vincentlin.togglebutton;import android.app.activity;import android.os.bundle;import  android.view.menu;import android.view.menuitem;import android.widget.compoundbutton;import  android.widget.compoundbutton.oncheckedchangelistener;import android.widget.imagebutton;import  android.widget.imageview;import android.widget.togglebutton;public class mainactivity  extends Activity implements OnCheckedChangeListener{ private ToggleButton  tbutton; private imageview iview;  @Override  protected void oncreate (Bundle  savedinstancestate)  {  super.oncreate (savedinstancestate);   setContentView ( R.layout.activity_main);   //initialization Component   tButton =  (ToggleButton)  findviewbyid ( R.id.togglebutton1);  iview =  (ImageView)  findviewbyid (r.id.imageview1);   / *   *  to the currentTButton Set Listener    */  tbutton.setoncheckedchangelistener (this);   }  @Override  public void oncheckedchanged (compoundbutton buttonview, boolean  isChecked)  {  /*   *    *  when TButton is clicked, the current method executes     * buttonview---represents the clicked control itself,    * ischecked---represents the state of the control being clicked    *   When clicking on this TButton, change the img background    */  iview.setbackgroundresource (isChecked? R.drawable.on:r.drawable.off);    }}
//

650) this.width=650; "style=" Float:none; "title=" Image Off.png "src=" http://s3.51cto.com/wyfs02/M01/58/A2/ Wkiom1s2iplhsxq_aadbigqucte871.jpg "alt=" Wkiom1s2iplhsxq_aadbigqucte871.jpg "/>

650) this.width=650; "style=" Float:none; "title=" Image On.png "src=" http://s3.51cto.com/wyfs02/M02/58/9F/ Wkiol1s2icchb0wlaadyssar6v4214.jpg "alt=" Wkiol1s2icchb0wlaadyssar6v4214.jpg "/>

Android Basics Two ToggleButton

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.