Checkboxes: Java Source Code

import android.widget.CheckBox;
A checkbox is a specific type of two-states button that can be either checked or unchecked.

if ( ((CheckBox) v).isChecked( ) ) {
The method is used to check whether the particular CheckBox is checked or unchecked.

StringBuffer result = new StringBuffer( );
This class is a modifiable sequence of characters for use in creating strings, where all accesses are synchronized. The majority of the modification methods on this class return this so that method calls can be chained together. For example:
   new StringBuffer("a").append("b").append("c").toString( )
Selecting
iPhone &
Android and

pushing
DISPLAY


MyApplication/app/src/main/java/com/example/wenchen/myapplication/MainActivity.java
01package com.example.wenchen.myapplication;
02 
03import android.app.Activity;
04import android.os.Bundle;
05import android.view.View;
06import android.view.View.OnClickListener;
07import android.widget.Button;
08import android.widget.CheckBox;
09import android.widget.Toast;
10 
11public class MainActivity extends Activity {
12  private CheckBox chkIos, chkAndroid, chkWindows;
13  private Button btnDisplay;
14 
15  @Override
16  public void onCreate( Bundle savedInstanceState ) {
17    super.onCreate( savedInstanceState );
18    setContentView( R.layout.activity_main );
19    addListenerOnChkIos( );
20    addListenerOnButton( );
21  }
22 
23  public void addListenerOnChkIos( ) {
24    chkIos = (CheckBox) findViewById( R.id.chkIos );
25    chkIos.setOnClickListener( new OnClickListener( ) {
26      @Override
27      public void onClick( View v ) {
28        // Is chkIos checked?
29        if ( ((CheckBox) v).isChecked( ) ) {
30          Toast.makeText( MainActivity.this,
31            "Bro, try Android :)", Toast.LENGTH_LONG ).show( );
32        }
33      }
34    });
35  }
36 
37  public void addListenerOnButton( ) {
38    chkIos     = (CheckBox) findViewById( R.id.chkIos     );
39    chkAndroid = (CheckBox) findViewById( R.id.chkAndroid );
40    chkWindows = (CheckBox) findViewById( R.id.chkWindows );
41    btnDisplay = (Button)   findViewById( R.id.btnDisplay );
42    btnDisplay.setOnClickListener( new OnClickListener( ) {
43      // Run when button is clicked.
44      @Override
45      public void onClick( View v ) {
46        StringBuffer result = new StringBuffer( );
47        result.append( "iPhone check: " ).append( chkIos.isChecked( ) );
48        result.append( "\nAndroid check: " ).append( chkAndroid.isChecked( ) );
49        result.append( "\nWindows Phone check: " ).append( chkWindows.isChecked( ) );
50        Toast.makeText( MainActivity.this, result.toString( ),
51          Toast.LENGTH_LONG ).show( );
52      }
53    });
54  }
55}