Android implementation of single, multiple choice operation _android

Source: Internet
Author: User
Tags gettext

The example of this article for everyone to share the Android implementation of a single, multiple options for the operation of the code, for your reference, the specific contents are as follows

1. Single Choice
1.1. Layout

<?xml version= "1.0" encoding= "Utf-8"?> <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.rj141.sb.kongjian.DateActivity" > <textvi EW android:layout_marginleft= "10DP" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "2+3=" android:textsize= "22DP"/> <radiogroup android:layout_marginleft= "20DP" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" > <radiobutton android:layout_width= "Wrap_c" Ontent "android:layout_height=" wrap_content "android:text=" A.2 "android:id=" @+id/rb1 "/> <RadioBu Tton android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "B.3" android:i d= "@+id/rb2"/> <radioButton android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "C.4" Android : id= "@+id/rb3"/> <radiobutton android:layout_width= "Wrap_content" android:layout_height= T "android:text=" D.5 "android:id=" @+id/rb4 "/> </RadioGroup> <button android:id=" @+id/sub MIT "android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:text=" Submit "/> </Linear Layout>

1.2.Java file

public class Singchoose extends Appcompatactivity { 
 private Button btn; 
 Private RadioButton RbD; 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
  super.oncreate (savedinstancestate); 
  Setcontentview (r.layout.sing_choose); 
 
  Rbd= (RadioButton) This.findviewbyid (R.ID.RB4); 
  Btn= (Button) This.findviewbyid (r.id.submit); 
  Btn.setonclicklistener (New View.onclicklistener () { 
   @Override public 
   void OnClick (View v) { 
    if ( Rbd.ischecked ()) { 
     Toast.maketext (singchoose.this, "correct, please add five Points", Toast.length_short). Show (); 
    else { 
     Toast.maketext (singchoose.this, "error, please subtract five Points", Toast.length_short). Show ();}}} 
  

Effect Chart:

2. Multi-item selection
2.1. Layout

<?xml version= "1.0" encoding= "Utf-8"?> <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.rj141.sb.kongjian.CheckChoose" > <textview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "20DP" Android:text = "Which of the following items do you like?" "/> <checkbox android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:text" = "Chocolate" android:id= "@+id/cb1"/> <checkbox android:layout_width= "wrap_content" android:layout_height= "WR" 
  Ap_content "android:text=" Ice cream android:id= "@+id/cb2"/> <checkbox android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Cake" android:id= "@+id/cb3"/> <checkbox Out_width= "Wrap_content" 
  android:layout_height= "wrap_content" android:text= "Beer" android:id= "@+id/cb4"/> <checkbox, Android:l 
 
 Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Meat" android:id= "@+id/cb5"/> 
  <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textSize= "18DP" 
 Android:id= "@+id/tv"/> </LinearLayout>

2.2.Java file

public class Checkchoose extends Appcompatactivity implements Compoundbutton.oncheckedchangelistener {private checkb 
 Ox cb1,cb2,cb3,cb4,cb5; 
 Private TextView TV; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Setcontentview (R.layout.check_choose); 
  tv= (TextView) This.findviewbyid (r.id.tv); 
  cb1= (CheckBox) This.findviewbyid (R.ID.CB1); 
  Cb2= (CheckBox) This.findviewbyid (R.ID.CB2); 
  cb3= (CheckBox) This.findviewbyid (R.ID.CB3); 
  cb4= (CheckBox) This.findviewbyid (R.ID.CB4); 
  cb5= (CheckBox) This.findviewbyid (R.ID.CB5); 
  Cb1.setoncheckedchangelistener (this); 
  Cb2.setoncheckedchangelistener (this); 
  Cb3.setoncheckedchangelistener (this); 
  Cb4.setoncheckedchangelistener (this); 
 Cb5.setoncheckedchangelistener (this); 
  @Override public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {String str= "you like:"; 
  if (cb1.ischecked ()) {Str+=cb1.gettext () + ","; } if (Cb2.ischEcked ()) {Str+=cb2.gettext () + ","; 
  } if (cb3.ischecked ()) {Str+=cb3.gettext () + ","; 
  } if (cb4.ischecked ()) {Str+=cb4.gettext () + ","; 
  } if (cb5.ischecked ()) {Str+=cb5.gettext () + ","; 
 } tv.settext (str);  } 
}

Effect Chart:

This is the entire content of this article, I hope to learn more about Android software programming help.

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.