Get CheckBox value : CheckBox « UI « Android






Get CheckBox value

 


package app.test;

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

public class Test extends Activity {
  CheckBox plain_cb;
  CheckBox serif_cb;
  CheckBox italic_cb;
  CheckBox bold_cb;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("CheckBoxActivity");
    setContentView(R.layout.main);
    find_and_modify_text_view();
  }

  private void find_and_modify_text_view() {
    plain_cb = (CheckBox) findViewById(R.id.plain_cb);
    serif_cb = (CheckBox) findViewById(R.id.serif_cb);
    italic_cb = (CheckBox) findViewById(R.id.italic_cb);
    bold_cb = (CheckBox) findViewById(R.id.bold_cb);
    Button get_view_button = (Button) findViewById(R.id.get_view_button);
    get_view_button.setOnClickListener(get_view_button_listener);
  }

  private Button.OnClickListener get_view_button_listener = new Button.OnClickListener() {
    public void onClick(View v) {
      String r = "";
      if (plain_cb.isChecked()) {
        r = r + "," + plain_cb.getText();
      }
      if (serif_cb.isChecked()) {
        r = r + "," + serif_cb.getText();
      }
      if (italic_cb.isChecked()) {
        r = r + "," + italic_cb.getText();
      }
      if (bold_cb.isChecked()) {
        r = r + "," + bold_cb.getText();
      }
      setTitle("Checked: " + r);
    }
  };
}


//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<CheckBox android:id="@+id/plain_cb"
     android:text="Plain"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
/>
 
<CheckBox android:id="@+id/serif_cb"
     android:text="Serif"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:typeface="serif"
/>
 
<CheckBox android:id="@+id/bold_cb"
     android:text="Bold"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textStyle="bold"
/>
 
<CheckBox android:id ="@+id/italic_cb"
     android:text="Italic"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textStyle="italic"
/>


  <Button android:id="@+id/get_view_button" 
      android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Get CheckBox" />
    
</LinearLayout>

   
  








Related examples in the same category

1.Listen to CheckBox action
2.Is CheckBox selected
3.flips the checkbox to unchecked if it was checked
4.Use choice mode on a list. This list is in CHOICE_MODE_SINGLE mode, which means the items behave like checkboxes.
5.Use choice mode on a list. This list is in CHOICE_MODE_MULTIPLE mode, which means the items behave like checkboxes.