isChecked()
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.view.View;
import android.util.Log;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox fishCB = (CheckBox)findViewById(R.id.fishCB);
fishCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.v("CheckBoxActivity", (isChecked ? "checked": "not checked"));
}
});
}
public void doClick(View view)
{
Log.v("CheckBoxActivity", ((CheckBox)view).isChecked() ? "checked" : "not checked");
}
}
activity_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/chickenCB" android:text="Chicken" android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<CheckBox android:id="@+id/fishCB" android:text="Fish"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<CheckBox android:id="@+id/steakCB" android:text="Steak" android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="doClick" />
</LinearLayout>