checkbox for Android Common components

Source: Internet
Author: User

In Android app Development, when a multi box is used, it can be implemented by the checkbox component.

The function of this instance is to select the option in the list, click the Submit button, and then pop up the dialog box to display the submission.

directly on the code.

The first is the layout file:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"     xmlns: tools= "Http://schemas.android.com/tools"     android:id= "@+id/linearlayout1"      android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:orientation= "Vertical"     tools:context= " Main.testa9.MainActivity " >    <TextView          android:id= "@+id/tv1"         android:layout_width= " Wrap_content "        android:layout_height=" Wrap_content "         android:text= "@string/choose"/>    <checkbox          android:id= "@+id/cb1"          android:layout_width= "Wrap_coNtent "        android:layout_height=" Wrap_content "         android:text= "@string/check_one"/>    <checkbox          android:id= "@+id/cb2"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:text= "@string/check_two"/>     <checkbox         android:id= "@+id/cb3 "        android:layout_width="      "Wrap_content"     android:layout_height= "Wrap_content"          android:text= "@string/check_three"/>    <button          android:iD= "@+id/button1"         android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:text= "@string/btn1"/></linearlayout>

Next is the Strings.xml file:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >TestA9</string> & Lt;string name= "BTN1" > Submit </string> <string name= "Check_one" >NO.1</string> <string name= "Chec K_two ">NO.2</string> <string name=" Check_three ">NO.3</string> <string name=" Choose "> Please select :</string></resources>

Finally, the Android source file:

package main.testa9;import android.support.v7.app.actionbaractivity;import  android.app.alertdialog;import android.content.dialoginterface;import android.os.bundle;import  android.view.view;import android.view.view.onclicklistener;import android.widget.button;import  android.widget.checkbox;public class mainactivity extends actionbaractivity  implements onclicklistener{    private button btn1=null;     private CheckBox cb1=null;    private CheckBox cb2=null;     private CheckBox cb3=null;     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (r.layout.activity_main);                btn1= (Button) findViewById (R.id.button1) ;         cb1= (CheckBox) Findviewbyid (R.ID.CB1);         cb2= (CheckBox) Findviewbyid (R.ID.CB2);         cb3= (CheckBox) Findviewbyid (R.ID.CB3);         btn1.setonclicklistener ( Mainactivity.this);    }     @Override     public  void onclick (view arg0)  {        string str= "";         if (cb1.ischecked ())  str= "the";         if (cb2.ischecked ())  str+= "No.2";         if (cb3.ischecked ())  str+= "No.3";               &nbsP;     new alertdialog.builder (mainactivity.this). SetTitle ("hint"). SetMessage (" Your choice is: \ n "+str). Setpositivebutton (" OK ", New dialoginterface.onclicklistener ()  {                  @Override              public void onclick (DIALOGINTERFACE&NBSP;ARG0, &NBSP;INT&NBSP;ARG1)  {                 //can add a button event listener, where you can return directly, set the parameter to null or the same effect                  return;             }        }). Show ();     }    }

Test results:

checkbox for Android Common components

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.