2015-03-05 22:15:04| 分类: Android 开发每日 | 标签: |举报 |字号大中小 订阅
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.CheckBox; import android.widget.CompoundButton; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox fishCB = (CheckBox) findViewById(R.id.fishCB); if (fishCB.isChecked()) { fishCB.toggle(); // flips the checkbox to unchecked if it was checked } fishCB.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { Log.v("CheckBoxActivity", "The fish checkbox is now " + (isChecked ? "checked" : "not checked")); } } ); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> /* ww w.jav a 2 s . co m*/ <CheckBox android:id="@+id/chickenCB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="XML" /> <CheckBox android:id="@+id/fishCB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CSS" /> <CheckBox android:id="@+id/steakCB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="HTML" /> </LinearLayout>
评论