Android self-paced note ToggleButton (switch Button) features, Special properties, usage

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size:18px"><span style="font-size:18px">1.ToggleButton properties:<br>1> has two states: checked and unchecked, and you need to set different display text for different states<br>2>android:checked= "true"<br>3>android:textoff= "off" (default State)<br>4>android:texton= "open"<br>2. How to Use: (example)<br>public class Mainactivity extends Activity implements oncheckedchangelistener{<br>1> Initialization Control<br>2> Assigning a value to a control<br>3> setting listeners to controls<br>4> overriding the OnCheckedChanged () method {<br>Executes when the control is clicked, ischecked represents the state of the clicked control<br>Imageview.setbackgroundresource (isChecked? r.drawable.on:r.drawable.off);<br>}<br></span></span><p><p><span style="font-size:18px">}</span></p></p><p><p><span style="font-family:KaiTi_GB2312; font-size:18px">Here's a look at the implementation of the specific code: for everyone to watch the convenience I set up two pictures, when the Open is a picture, off the time is 21 pictures</span></p></p><p><p><span style="font-family:KaiTi_GB2312; font-size:18px">The first is: Activity_main.xml</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"><span style= "font-family:kaiti_gb2312;" ><togglebutton android:checked= "false" android:texton= "open" android:textoff= "off" android: Layout_width= "match_parent" android:layout_height= "wrap_content" android:id= "@+id/togglebutton" / > <imageview android:layout_width= "match_parent" android:layout_height= "match_parent " android:id= "@+id/imageview" android:layout_below= "@+id/togglebutton" android:background= "@drawable/ On " /></span></pre></pre><br>The last is: Mainactivity.class<p><p></p></p><p><p><span style="font-size:18px"></span></p></p><pre name="code" class="java"><span style= "font-family:kaiti_gb2312;" >package Com.example.administrator.togglebutton1;import Android.content.dialoginterface;import Android.media.image;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.compoundbutton;import Android.widget.imageview;import Android.widget.togglebutton;public class Mainactivity extends ActionBarActivity Implements Compoundbutton.oncheckedchangelistener {private ToggleButton tb; Private ImageView img; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TB = (ToggleButton) Findviewbyid (r.id.togglebutton); Initialize img = (ImageView) Findviewbyid (r.id.imageview); Tb.setoncheckedchangelistener (this); Set listener for control} @Override public void OnCheckedChanged (compoundbutton buttonview, boolean isChecked) {//override Onche CkEdchanged () Method//when The control is clicked, isChecked represents the state of the control being clicked Img.setbackgroundresource (isChecked? r.drawable.on:r.drawable.ic_adc); }}</span></pre><span style="font-family:KaiTi_GB2312"><span style="font-family:KaiTi_GB2312"><br></span></span><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android self-paced note ToggleButton (switch Button) features, Special properties, usage</p></p></span>

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.