Example of a simple use checkbox for Android development _android

Source: Internet
Author: User
Tags gettext sqlite database stub

This example describes the simple use of a checkbox in Android development. Share to everyone for your reference, specific as follows:

checkbox is a common control in the development of the interface, Android UI development also has a checkbox, simple to say that it is used, each checkbox to set up listening, The listener set is Compoubutton.oncheckedchangedlistener ().

Package Com.zhuguangwei;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.CheckBox;
Import Android.widget.CompoundButton;
Import Android.widget.TextView;
  public class Chkboxactivity extends activity {private TextView Mytextview;
  Private CheckBox myapple;
  Private CheckBox Myorange;
  Private CheckBox Mybanana;
  Private CheckBox Mywatermelon;
  Private CheckBox Mystrawberry; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Find TextView Mytextview = (TextView) Findviewbyid (r.id.mytextview) by ID;
    Find these CheckBox myapple = (CheckBox) Findviewbyid (r.id.apple) by ID;
    Myorange = (CheckBox) Findviewbyid (R.id.orange);
    Mybanana = (CheckBox) Findviewbyid (R.id.banana);
    Mywatermelon = (CheckBox) Findviewbyid (R.id.watermelon);
    Mystrawberry = (CheckBox) Findviewbyid (R.id.strawberry); Myapple.sEtoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void Oncheckedchange
          D (Compoundbutton Buttonview, Boolean ischecked) {//TODO auto-generated Method stub if (ischecked) {
        Mytextview.append (Myapple.gettext (). toString ()); else{if (Mytextview.gettext (). ToString (). Contains ("Apple")) {Mytextview.settext (mytextview.ge
          Ttext (). toString (). replace ("Apple", ""));
    }
        }
      }
    }); Myorange.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void Onche Ckedchanged (Compoundbutton Buttonview, Boolean ischecked) {//TODO auto-generated Method stub if (Ischecke
        d) {mytextview.append (Myorange.gettext (). toString ()); else{if (Mytextview.gettext (). ToString (). Contains ("orange")) {Mytextview.settext (mytextview.ge
          Ttext (). toString (). replace ("Orange", ""));
   }     }
      }
    }); Mybanana.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void Onche Ckedchanged (Compoundbutton Buttonview, Boolean ischecked) {//TODO auto-generated Method stub if (Ischecke
        d) {mytextview.append (Mybanana.gettext (). toString ()); else{if (Mytextview.gettext (). ToString (). Contains ("banana")) {Mytextview.settext (mytextview.ge
          Ttext (). toString (). Replace ("banana", ""));
    }
        }
      }
    }); Mywatermelon.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void O Ncheckedchanged (Compoundbutton Buttonview, Boolean ischecked) {//TODO auto-generated Method stub if (Isch
        ecked) {mytextview.append (Mywatermelon.gettext (). toString ()); else{if (Mytextview.gettext (). ToString (). Contains ("Watermelon")) {Mytextview.settext (mytextview.ge Ttext ().ToString (). Replace ("Watermelon", ""));
    }
        }
      }
    }); Mystrawberry.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void O Ncheckedchanged (Compoundbutton Buttonview, Boolean ischecked) {//TODO auto-generated Method stub if (Isch
        ecked) {mytextview.append (Mystrawberry.gettext (). toString ()); else{if (Mytextview.gettext (). ToString (). Contains ("Strawberry")) {Mytextview.settext (mytextview.ge
          Ttext (). toString (). Replace ("Strawberry", ""));
  }
        }
      }
    });

 }
}

The

Main.xml file content is:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" "Fill_parent"
  ; <textview android:id= "@+id/mytextview" android:layout_width= fill_parent "android:layout_height=" Wrap_conte NT "android:text=" Please choose your favorite fruit: "/> <checkbox android:id=" @+id/apple "android:layout_width=" Wrap_co
    Ntent "android:layout_height=" wrap_content "android:text=" apple "/> <checkbox android:id=" @+id/Orange " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Orange"/> <Check Box android:id= "@+id/banana" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andr oid:text= "Banana"/> <checkbox android:id= "@+id/watermelon" android:layout_width= "Wrap_content" Android:
layout_height= "Wrap_content"    android:text= "Watermelon"/> <checkbox android:id= "@+id/strawberry" android:layout_width= "Wrap_content"

 android:layout_height= "wrap_content" android:text= "Strawberry"/> </LinearLayout>

The results of the operation are:

More interested readers of Android-related content can view the site: "Summary of the usage of Android controls", "Summary of operational techniques for Android Programming", "Android View tips Summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and Android development introductory and advanced tutorials

I hope this article will help you with the Android program.

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.