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
package com.example.wenchen.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {
  private CheckBox chkIos, chkAndroid, chkWindows;
  private Button btnDisplay;

  @Override
  public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    addListenerOnChkIos( );
    addListenerOnButton( );
  }

  public void addListenerOnChkIos( ) {
    chkIos = (CheckBox) findViewById( R.id.chkIos );
    chkIos.setOnClickListener( new OnClickListener( ) {
      @Override
      public void onClick( View v ) {
        // Is chkIos checked?
        if ( ((CheckBox) v).isChecked( ) ) {
          Toast.makeText( MainActivity.this,
            "Bro, try Android :)", Toast.LENGTH_LONG ).show( );
        }
      }
    });
  }

  public void 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( ) {
      // Run when button is clicked.
      @Override
      public void onClick( View v ) {
        StringBuffer result = new StringBuffer( );
        result.append( "iPhone check: " ).append( chkIos.isChecked( ) );
        result.append( "\nAndroid check: " ).append( chkAndroid.isChecked( ) );
        result.append( "\nWindows Phone check: " ).append( chkWindows.isChecked( ) );
        Toast.makeText( MainActivity.this, result.toString( ),
          Toast.LENGTH_LONG ).show( );
      }
    });
  }
}