In this tutorial, we will show you how to create three check boxes in an XML file and demonstrate the status of the listener’s use check – checked or unchecked check boxes.

P.S. this project was developed in Eclipse 3.7 and tested with Android 2.3.3.

1. User-defined string Open res/values/strings. XML file, add some user-defined string.

Res/values/strings. XML file:

< resources> < string name = “hello” >Hello World, MyAndroidAppActivity! < / string> < string name = “app_name” >MyAndroidApp < / string> < string name = “chk_ios” >IPhone < / string> < string Name = “CHk_android” >Android < / string> < string Name = “CHk_Windows” >Windows Mobile < / string> < string Name = “btn_display” >Display < / string> < / resources> 2. Open “res/layout/ main.xml” file, add 3 “check boxes” and a button, inside the linear layout.

File: res/layout/ main.xml

< LinearLayout XMLNS: android = “schemas.android.com/apk/res/and… Android orientation= “vertical” > android layout_width= “fill_parent”

< CheckBox android ID = "@+id/chkIos" Android Layout_width = "wrap_content" Android Layout_height = "wrap_content" Android text= "@string/chk_ios" > < CheckBox Android ID = "@+id/chkAndroid" Android layout_width= "wrap_content" android Layout_height = "wrap_content" Android text= "@string/chk_android" Android checked=true"> < CheckBox android ID =" @+id/chkWindows "Android Layout_width =" wrap_content "android Layout_height =" wrap_content" < Button iD = "@+id/btnDisplay" Android layout_width= "wrap_content" Android layout_height= "wrap_content" android text= "@string/btn_display" >Copy the code

< / LinearLayout> make the checkbox selected by default Put android:checked=”true” inside checkbox element to make it checked bu default. In this Case, “Android” option is checked by default. Attach inside your activity “onCreate ()” method, to monitor following events:

If checkbox ID: “chkios” is checked, display a floating box with message “Bro, try Android”. Displays a float box and check box status display if the button is clicked. File: myandroidappactivity. Java

Travel bag com. Mkyong. android

Import of android. The app. Activity imports Android. OS. Bundle imports Android. The view. View imports Android. The view. The View. OnClickListener imports Android. The widget. Button imports Android. The widget. CheckBox imports Android. The widget. Toast

Public myAndroidAppActivity extension activity {

Private CheckBox chkIos, chkAndroid, chkWindows private Button btnDisplay

Override Public invalid creation callback (Bundle savedInstanceState) {super. Create callback function (savedInstanceState) setContentView (R. Layout. The main)

Addlisteneronchkios () addListeneronButton ()Copy the code

}

Public invalid addListeneronchkios () {

ChkIos = (CheckBox) findViewById (R. Id. ChkIos chkIos). Setonclicklistener (new onClickListener () {@override public invalid onclick (View v) {/ / Is chkios checked? If (((CheckBox) v). Check ()) {Toast. Maketext (MyAndroidAppActivity. This, "bro, try Android :)", Toast. LENGTH_LONG). Trade show ()}}})Copy the code

}

Public invalid addListeneronButton () {

ChkIos = (CheckBox) findViewById (R. Id. ChkIos) chkAndroid = (CheckBox) findViewById (R. Id. ChkAndroid) chkWindows = (CheckBox) findViewById (R. Id. ChkWindows) btnDisplay = (Button) findViewById (R). Id. BtnDisplay btnDisplay). Setonclicklistener (new onClickListener () {clicked button is when/clicked @override public void onClick (View v) {StringBuffer Result = new StringBuffer () result. Append (" iPhone Check: "). Append (chkIos. () result. Append (" nAndroid check: "). Append (chkAndroid. () result. Append (" Move NWindows check: \ "). Append (chkWindows. Toast. Maketext (MyAndroidAppActivity. This result. toString(), Toast.LENGTH_LONG).show(); }});Copy the code

}}

4. Demo

Run the application.

1. Result :

android checkbox demo 1

2. If “IPhone” is checked:

android checkbox demo2

Checked “IPhone” and “Windows Mobile”, later, click on the “display” button: 3. Checked “IPhone” and “Windows Mobile”, later, click on the “display” button:

android checkbox demo3



www.apkbus.com/blog-919651…