Initial knowledge of Android applet (switch light-single-Select multi-select button control)

Source: Internet
Author: User

Click the radio button "Turn on the Light", the multi-select button will show "Off the Light" and the box has a tick; Conversely, click the multi-select button, the radio button also automatically changed.

First, create an Android project (my version is 4.4.2), the name of "bulb", two pictures: Turn on the lights and lights off the status of the picture into the "drawable-" Random folder

Then find the Layout folder under the Res folder, find Activity_main.xml or fragment_main.xml, enter or drag the button

<relativelayout 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 "Tools:context =". Mainactivity "> <imageview android:id=" @+id/image "android:layout_width=" 120DP "Android:layo ut_height= "120DP" android:layout_alignparenttop= "true" Android:layout_centerhorizontal= "true" Android : src= "@drawable/off"/> <radiogroup android:id= "@+id/radiogroup1" android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "android:layout_alignright=" @+id/image "android:layout_below=" @+id/image "android:layout_marginright=" 35DP "android:layout_margintop=" 20DP "android:orientation=" H Orizontal "> <radiobutton android:id=" @+id/on "android:layout_width=" Wrap_conten T "android:layout_height=" WRAP_Content "android:text=" turn on the Light "/> <radiobutton android:id=" @+id/off "a            Ndroid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:checked= "true" Android:text= "Off light"/> </RadioGroup> <checkbox android:id= "@+id/checkbulb11" Androi        D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/radioGroup1" Android:layout_centerhorizontal= "true" android:layout_margintop= "34DP" android:text= "Turn on the Light"/></rel Ativelayout>

Finally, in the Java file under src Mainactivity.java

Package Com.example.bulb;import Android.app.activity;import Android.os.bundle;import android.view.menu;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.imageview;import Android.widget.radiobutton;public class Mainactivity extends Activity implements Oncheckedchangelistener{private ImageView image;private RadioButton on,off;private CheckBox checkbulb; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.fragment_main); Image= (ImageView ) This.findviewbyid (R.id.image), on= (RadioButton) This.findviewbyid (R.id.on); off= (RadioButton) This.findviewbyid ( R.id.off); checkbulb= (CheckBox) This.findviewbyid (R.ID.CHECKBULB11); On.setoncheckedchangelistener (this); Checkbulb.setoncheckedchangelistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuiNflater (). Inflate (R.menu.main, menu); return true;} public void Setbulbstate (Boolean state) {if (state==true) {//Change picture Image.setimageresource (R.drawable.on);// Change the checkbox text Checkbulb.settext ("Off the Lights");} else{//Change the picture Image.setimageresource (R.drawable.off);//Change the checkbox text Checkbulb.settext ("Turn On the Light");} Change RadioButton status on.setchecked (state); off.setchecked (!state);//Change Chackbox status checkbulb.setchecked (state);} @Overridepublic void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {setbulbstate (arg1);}}

The effect of self-test it!



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.